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. avaScript中变量的声明和赋值

    变量是指程序中一个已经命名的存储单元,它的主要作用就是为数据操作提供存放信息的容器.变量是相对常量而言的.常量是一个不会改变的固定值,而变量的值可能会随着程序的执行而改变.变量有两个基本特征,即变量名 ...

  2. html5 web worker学习笔记(记一)

    (吐槽:浏览器js终于进入多线程时代!) 以前利用setTimeout.setInterval等方式的多线程,是伪多线程,本质上是一种在单线程中进行队列执行的方式.自从html5 web worker ...

  3. SLAM: Structure From Motion-移动中三维场景重建

    wiki链接:https://en.wikipedia.org/wiki/Structure_from_motion 三维重建: 三维物体建模总结 1. 视野内三维物体重建 : Kinect fusi ...

  4. OpenCV实现灰度直方图和直方图拉伸

    原文链接:http://blog.csdn.net/xiaowei_cqu/article/details/7600666 如有疑问或者版权问题,请移步原作者或者告知本人. 灰度直方图是数字图像中最简 ...

  5. C++的Android接口---配置NDK

    一. 在安卓工具网站下载ADT:http://tools.android-studio.org/index.php 参考链接:http://1527zhaobin.iteye.com/blog/186 ...

  6. 如何安全使用dispatch_sync

    概述 iOS开发者在与线程打交道的方式中,使用最多的应该就是GCD框架了,没有之一.GCD将繁琐的线程抽象为了一个个队列,让开发者极易理解和使用.但其实队列的底层,依然是利用线程实现的,同样会有死锁的 ...

  7. Html-如何正确给table加边框

    一般来说,给表格加边框都会出现不同的问题,以下是给表格加边框后展现比较好的方式 <style> table,table tr th, table tr td { border:1px so ...

  8. 【剑指Offer】55、链表中环的入口结点

      题目描述:   给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null.   解题思路:   本题是一个比较典型的链表题目,难度适中.首先,对于大多人来说,看到这道题是比较开心的 ...

  9. 洛谷P1339 [USACO09OCT]热浪Heat Wave

    思路:裸SPFA过一遍(建议使用邻接链表存储),无向图,无向图,无向图,重要的事情要说三遍!!!蜜汁RE是什么鬼????第九个点数组开到20K,第十个点数组开到30K才AC.或许我代码写的有bug?( ...

  10. Problem 20

    Problem 20 问题网址:https://projecteuler.net/problem=20 n! means n × (n − 1) × ... × 3 × 2 × 1阶乘For exam ...