1. typeof 操作符
  2. instanceof 操作符
  3. hasOwnProperty()方法
  4. isPrototypeOf()方法

  1.typeof

  用于获取变量的类型,一般只返回以下几个值:string,number,boolean,object,function,undefined.

  typeof作用于引用类型的时候,无论是什么类型的对象,都返回“object”. 

     alert(typeof false);//boolean
alert(typeof );//number
alert(typeof "hello Jame");
alert(typeof null);//object
alert(typeof [,,,]);//object
alert(typeof {name:"Jhon",age:});//object
alert(typeof function a(){});//function
alert(typeof new String("str"));//object

  String 与 string的区别:一个是引用类型 object,一个是值类型 string。

     var s = "hello Jame";
var Str = new String("hello Jhon");
alert(typeof s);//string
alert(typeof Str);//object

  凡是通过 new 操作符创建的变量,typeof运算的结果都是object.

     function myFun (){};
var a = new myFun();
alert(typeof a);//object
var b = function myFun1 (){};
alert(typeof b);//function
alert(typeof myFun1);//undefined

  2.instanceof

  用来判断一个实例是否属于某种类型。

  更重的一点是 instanceof 可以在继承关系中用来判断一个实例是否属于它的父类型。

     function ClassA(){}
  function ClassB(){}
  function ClassC(){}
  /**
   * 原型继承
   * ClassB继承自ClassA,ClassC继承自ClassB
   */
   ClassB.prototype = new ClassA();
   ClassC.prototype = new ClassB();      /**
   * 创建ClassB和ClassC的实例
   */
   var b = new ClassB();
   var c = new ClassC();
   alert(b instanceof ClassB);//true
   alert(b instanceof ClassA);//true
   alert(c instanceof ClassA);//true
   alert(c instanceof ClassB);//true
   alert(c instanceof ClassC);//true

  instanceof复杂用法

    alert(Object instanceof Object);//true
    alert(Function instanceof Function);//true   alert(Function instanceof Object);//true   alert(String instanceof String);//false
  alert(Number instanceof Number);//false
  alert(Boolean instanceof Boolean);//false

  要从根本上了解instanceof,需从以下两个方面着手:1、语言规范中如何定义该运算符。2、JavaScript的原型继承机制。

  推荐文章:JavaScript instanceof 运算符深入剖析

  3.hasOwnProperty()方法

  用来判断某实例是否具指定的名称属性或方法。是返回true,否则返回false。

   注意:该属性只有是对象本身的一个成员才会返回true。不会检查原型链中是否有给定的方法或属性。

  IE 5.5+、FireFox、Chrome、Safari、Opera等主流浏览器均支持该函数。

      function ClassA(){
  this.fname="张";
  this.lname="三";
  this.rank = ;
  this.sayHello = function(){
  alert("Hello:"+fname+lname);
  };
  }   var myPro = {
  name:"张三",
  age:
  };
  ClassA.prototype = myPro;
  var a = new ClassA();
  alert(a.hasOwnProperty("fname"));//true
  alert(a.hasOwnProperty("lname"));//true
   alert(a.hasOwnProperty("sayHello"));//true
   alert(a.hasOwnProperty("name"));//false

  4.isPrototypeOf()方法

  判断某对象是否是指定实例的原型。是返回true,否则返回false。

  注意:该方法不会检查原型链中的对象。即:只检查本实例的直接原型,不会检查本实例的原型的原型。

      function ClassA(){}
  function ClassB(){}
  function ClassC(){}
  /**
  * ClassB的原型设为ClassA,ClassC的原型设为ClassB
   * 注意:这与原型继承不同
   */
  ClassB.prototype = ClassA;
   ClassC.prototype = ClassB;
   var b = new ClassB();
   var c = new ClassC();
   alert(ClassA.isPrototypeOf(b));//true
   alert(ClassB.isPrototypeOf(c));//true
   alert(ClassA.isPrototypeOf(c));//false

  prototype和_proto_相连的两部分,用isPrototypeOf(),结果返回true.请结合原型链图体会下列代码的结果。

     alert(Object.isPrototypeOf(Function));//false
alert(Object.prototype.isPrototypeOf(Function.prototype));//true function ClassD(){};
alert(Object.prototype.isPrototypeOf(ClassD.prototype));//true
alert(Function.prototype.isPrototypeOf(ClassD));//true var d = new ClassD();
alert(Object.prototype.isPrototypeOf(d.prototype));//false
alert(Object.isPrototypeOf(d));//false var obb = new Object();
alert(Object.prototype.isPrototypeOf(Object));//true
alert(Object.prototype.isPrototypeOf(obb));//true

  引用:JavaScript instanceof 运算符深入剖析

  

typeof、instanceof、hasOwnProperty()、isPrototypeOf()的更多相关文章

  1. JavaScript中typeof,instanceof,hasOwnProperty,in的用法和区别

    一. typeof操作符 typeof操作符用于返回正在使用值的类型. // 使用原始值 let mNull = null; let mUndefined = undefined; let mStri ...

  2. java基础9 main函数、this、static、super、final、instanceof 关键字

    一.main函数详解 1.public:公共的.权限是最大的,在任何情况都可以访问  原因:为了保证jvm在任何情况下都可以访问到main法2.static:静态,静态可以让jvm调用更方便,不需要用 ...

  3. 面向对象的程序设计(二)理解各种方法和属性typeof、instanceof、constructor、prototype、__proto__、isPrototypeOf、hasOwnProperty

    //理解各种方法和属性typeof.instanceof.constructor.prototype.__proto__.isPrototypeOf.hasOwnProperty. //1.typeo ...

  4. typeof、instanceof与isPrototypeOf()的差异与联系

    一.typeof 1.typeof的意义及作用: 我们知道ECMAScript中有5种简单(基本)数据类型:Undefined.Null.Boolean.Number.String,以及一种引用数据类 ...

  5. constructor、prototype、isPrototypeOf、instanceof、in 、hasOwnProperty

    constructor.prototype.isPrototypeOf.instanceof.in .hasOwnProperty等等 constructor:对象构造器.存在于原型对象中?,相当于p ...

  6. JavaScript中typeof、toString、instanceof、constructor与in

    JavaScript 是一种弱类型或者说动态语言.这意味着你不用提前声明变量的类型,在程序运行过程中,类型会被自动确定. 这也意味着你可以使用同一个变量保存不同类型的数据. 最新的 ECMAScrip ...

  7. 【JavaScript中typeof、toString、instanceof、constructor与in】

    JavaScript中typeof.toString.instanceof.constructor与in JavaScript 是一种弱类型或者说动态语言.这意味着你不用提前声明变量的类型,在程序运行 ...

  8. javascript 中isPrototypeOf 、hasOwnProperty、constructor、prototype等用法

    hasOwnProperty:是用来判断一个对象是否有你给出名称的属性或对象,此方法无法检查该对象的原型链中是否具有该属性,该属性必须是对象本身的一个成员. isPrototypeOf是用来判断要检查 ...

  9. indexOf、instanceOf、typeOf、valueOf详解

    1.indexOf() 该方法用来返回某个指定的字符串值在字符串中首次出现的位置. 语法:indexOf(searchvalue,fromindex);两个参数,参数一表示查询的字符串值,参数二可选表 ...

随机推荐

  1. Android Studio and Gradle安装心得

    安装基于Eclipse 的ADT一段时间,感觉确实有很多功能不足,通过网上资料,决定改向AS. AS下载了最新的2.3版本,它不分64位与32位,网上说有单独版是瞎扯蛋.只要启动不同的EXE就行了. ...

  2. Spring MVC 中的基于注解的 Controller(转载)

           终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法 ...

  3. HDU_1269_tarjan求强连通分量

    迷宫城堡 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  4. mac nwjs入门

    NW.js由node-webkit项目发展而来其实很多东西官网上都有.但是鉴于搜索引擎(百度,google)搜索到的相关文章,让人看的很不明白.所以决定写下此篇文章. 官网:https://nwjs. ...

  5. Python字符串格式化--formate()的应用

    1.简单运用字符串类型格式化采用format()方法,基本使用格式是:转自 <模板字符串>.format(<逗号分隔的参数>) 调用format()方法后会返回一个新的字符串, ...

  6. Mysql [Err] 1118 - Row size too large

    首先声明,对MySQL不懂,很多都不知道原因 设计了一个表,里面很多text字段,然后填进去的东西太多(用的是Python的MySQLdb),报错: _mysql_exceptions.Operati ...

  7. vue 强制刷新组件

    <component v-if="hackReset"></component> 2 3 4 this.hackReset = false this.$ne ...

  8. eas之数据融合

    1.如何进行自由融合自由融合无须指定区域,KDTable将根据指定的融合模式,融合相邻且值相等的单元.// 自由行融合table.getMergeManager().setMergeMode(KDTM ...

  9. UNIX C 进程Part2

    1.获取进程ID #include <unistd.h> pid_t getpid(void); //获取子进程id pid_t getppid(void);//获取父进程id 2.获取实 ...

  10. C++ 对象创建的问题

    一.C++对象的创建: 对象创建的注意事项: 1.对象数组里的个数,就是创建对象的个数,普通数组一样:下标从0 到数组里数字 -1: 2.类名*  对象指针   <-->  这里只是一个指 ...