zoukankan      html  css  js  c++  java
  • 详解Object.constructor

    对象的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
    
  • 相关阅读:
    Python第二
    Python第一讲以及计算机基础
    MySQL第五讲
    MySQL第四讲
    MySQL第三讲
    MySQL第一讲概论
    MySQL日常笔记第二讲
    Linux修改用户组
    XAMPP中proftpd的简明配置方法
    解决php configure: error: Cannot find ldap libraries in /usr/lib.错误
  • 原文地址:https://www.cnblogs.com/wyangnb/p/8305897.html
Copyright © 2011-2022 走看看