zoukankan      html  css  js  c++  java
  • constructor object的区别

    对象的constructor属性引用了该对象的构造函数.对于 Object 对象,该指针指向原始的 Object() 函数.如下:

    var obj = {};
    obj.constructor //ƒ Object() { [native code] }
    obj.constructor == Object //true
    var arr = [];
    arr.constructor //ƒ Array() { [native code] }
    arr.constructor == Array //true
    function Fun(){
    console.log('function');
    }
    var fun = new Fun(); //实例化
    fun.constructor //ƒ Fun(){console.log('function')} 【打印出来的引用是Fun函数,说明fun的引用是Fun函数】
    Fun.constructor //ƒ Function() { [native code] } 【打印出来的引用是Funcion函数,说明Fun的引用是Function函数】
    fun.constructor == Fun //true 【再次证明fun的constructor属性引用了fun对象的构造函数】
    fun.constructor == Fun.constructor //false

    constructor常用于判断未知对象的类型,如下:

    function isArray (val){
    var isTrue = typeof val == 'object' && val.constructor == Array;
    return isTrue?true:false;
    }
    var arr = isArray([1,2,3,4,5]);
    console.log(arr); //true

    参考自https://www.cnblogs.com/xcee/

  • 相关阅读:
    剔除list中相同的结构体数据
    API Gateway微服务
    Webpack vs Gulp
    客服系统
    利用 istio 来对运行在 Kubernetes 上的微服务进行管理
    微服务环境搭建
    简易的开发框架(微服务) Asp.Net Core 2.0
    Istio官方文档中文版
    Hangfire Net Core2
    IIS7性能优化
  • 原文地址:https://www.cnblogs.com/LLLL-MM/p/10124713.html
Copyright © 2011-2022 走看看