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. Java数据类型转换1

    1 如何将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([ ...

  2. php判断方法及区别

    php判断方法 ‘is_类型名称’    php判断方法 $x="1"; echo gettype(is_string($x)); isset    是否存在 empty   是否 ...

  3. DHCP 和 MDT 分开服务器的设置方法

    DHCP设置 043:供应商特定信息:01 04 00 00 00 00 FF 060:PXEClient:PXEClient 066:启动服务器主机名:IP 067:启动文件名:\Boot\x86\ ...

  4. dubbo之异步调用

    异步调用 基于 NIO 的非阻塞实现并行调用,客户端不需要启动多线程即可完成并行调用多个远程服务,相对多线程开销较小. 在 consumer.xml 中配置: <dubbo:reference ...

  5. (转)基于Metronic的Bootstrap开发框架经验总结(7)--数据的导入、导出及附件的查看处理

    http://www.cnblogs.com/wuhuacong/p/4777720.html 在很多系统模块里面,我们可能都需要进行一定的数据交换处理,也就是数据的导入或者导出操作,这样的批量处理能 ...

  6. monad - the Category hierachy

    reading the "The Typeclassopedia" by Brent Yorgey in Monad.Reader#13 ,and found that " ...

  7. python 生成测试报告并发送邮件

    前言: 使用unittest编写自动化测试脚本,执行脚本后可以很方便看到测试用例的执行情况. 但如果想向领导汇报工作,就需要提供更直观的测试报告. 思路: 使用unittest编写测试用例,HTMLT ...

  8. Having子句用法

    Having基础用法 集合结果指定条件 注:HAVING子句中能够使用三种要素:常数,聚合函数,GROUP BY子句中指定的列名(聚合建) HAVING子句: 用having就一定要和group by ...

  9. AdminLTE框架基础布局使用

    boxbox-solid:去掉顶部边框线box-headerwith-border:添加头底部边框线 按钮:—— btn btn-default 默认<div class="btn-g ...

  10. 如何在Loadrunner11中解决HTTP BASIC认证登录报401的问题

    在对Carte+kettle的性能测试过程中,通过在loadrunner中用web_set_user("cluster", "cluster","17 ...