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

  • 相关阅读:
    VUE中引入zTree
    如何获取别人提供的接口,获取他接口里面的数据。
    com.fasterxml.jackson.databind.exc.InvalidDefinitionException
    2.Elasticsearch环境安装配置
    1.Elasticsearch概述
    Java中如何操作Redis
    基于Redis实现分布式锁
    Mybatis插件--数据库读写分离
    Mybatis插件--自定义分页
    7. Mybatis日志
  • 原文地址:https://www.cnblogs.com/cfinder010/p/3452285.html
Copyright © 2011-2022 走看看