zoukankan      html  css  js  c++  java
  • 关于javascript中的typeof和instanceof介绍

    typeof用来检测给定变量的数据类型 instanceof用来检测对象的类型


    typeof用来检测给定变量的数据类型(也可叫做基本类型,基本数据类型。包含undefined、boolean、string、number、object、function)
    var message = "so easy";
    alert(typeof message); //"string"
    alert(typeof 12); //"number"
    可以这样记忆:typeof是用来判断不是用new创建的“变量”。
    instanceof用来检测对象的类型(也可叫做引用类型。包含Object、Array、Date、RegExp、Function、基本包装类型(含Boolean、Number、String))
    var numberObject = new Number(10);
    var numberValue = 10; // www.jbxue.com
    alert(typeof numberObject); //"object"
    alert(typeof numberValue); //"number"
    alert(numberObject instanceof Number); //true
    alert(numberValue instanceof Number); //false
    numberValue是number基础数据类型,不属于任何引用类型。
    numberObject是object基础数据类型,属于Number引用类型(所有引用类型都从Object引用类型继承而来)。
    可以这样记忆:instanceof检测的都是用new创建的“对象”。而没有通过new创建出来的“变量”不属于任何一个引用类型。用typeof检测用new创建的“对象”始终返回的是“object引用类型”.
    isPrototypeOf()方法用来检测原型和实例的关系。instanceof同样也可以检测。只要是原型链中出现过的原型,都可以说是该原型链所派生的实例的原型。
    var person = new Person(); //Person继承与Object
    alert(Person.prototype.isPrototypeOf(person)); //true
    alert(Object.prototype.isPrototypeOf(person)); //true

  • 相关阅读:
    c语言 9-3
    c语言 9-12
    c语言 9-11
    c语言 9-10
    c语言中实现字符串大小写的转换
    tyvj1106 登山
    tyvj1125 JR's chop
    tyvj1148 小船弯弯
    tyvj1087 sumsets
    tyvj1086 Elevator
  • 原文地址:https://www.cnblogs.com/cfinder010/p/3452285.html
Copyright © 2011-2022 走看看