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://blog.csdn.net/wyangonly/article/details/79089881

  • 相关阅读:
    Sikulix 多个相似图片的选择
    Sikulix选取相对位置的图片或对象
    Sikulix 实用方法
    两个Excel内容比较
    SIkulix在Eclipse中的使用
    Sikulix IDE简介
    安装Sikulix
    Sikuli简介
    建立连接ALM的xml config文件
    XML序列化成对象
  • 原文地址:https://www.cnblogs.com/xcee/p/9934443.html
Copyright © 2011-2022 走看看