详解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
原文地址:https://www.cnblogs.com/wyangnb/p/8305897.html