zoukankan      html  css  js  c++  java
  • Object.prototype.constructor

    Returns a reference to the Object function that created the instance's prototype.

    注意这个属性的值是函数本省的引用,而不是一个包含函数名的字符串。这个值只有对primitive values(例如1,true和"test"等)才是只读的。

    Description

    All objects inherit a constructor property from their prototype:

    var o = {};
    o.constructor === Object; // true
    
    var a = [];
    a.constructor === Array; // true
    
    var n = new Number(3);
    n.constructor === Number; // true

    Examples

    Displaying the constructor of an object

    The following example creates a prototype, Tree, and an object of that type, theTree. The example then displays the constructor property for the object theTree.

    function Tree(name) {
      this.name = name;
    }
    
    var theTree = new Tree('Redwood');
    console.log('theTree.constructor is ' + theTree.constructor);

    This example displays the following output:

    theTree.constructor is function Tree(name) {
      this.name = name;
    }

    Changing the constructor of an object

    The following example shows how to modify constructor value of generic objects. Only true1and "test" will not be affected as they have read-only native constructors. This example shows that it is not always safe to rely on the constructor property of an object.

    function Type () {}
    
    var types = [
      new Array(),
      [],
      new Boolean(),
      true,             // remains unchanged
      new Date(),
      new Error(),
      new Function(),
      function () {},
      Math,
      new Number(),
      1,                // remains unchanged
      new Object(),
      {},
      new RegExp(),
      /(?:)/,
      new String(),
      'test'            // remains unchanged
    ];
    
    for (var i = 0; i < types.length; i++) {
      types[i].constructor = Type;
      types[i] = [types[i].constructor, types[i] instanceof Type, types[i].toString()];
    }
    
    console.log(types.join('
    '));

    This example displays the following output:

    function Type() {},false,
    function Type() {},false,
    function Type() {},false,false
    function Boolean() {
        [native code]
    },false,true
    function Type() {},false,Mon Sep 01 2014 16:03:49 GMT+0600
    function Type() {},false,Error
    function Type() {},false,function anonymous() {
    
    }
    function Type() {},false,function () {}
    function Type() {},false,[object Math]
    function Type() {},false,0
    function Number() {
        [native code]
    },false,1
    function Type() {},false,[object Object]
    function Type() {},false,[object Object]
    function Type() {},false,/(?:)/
    function Type() {},false,/(?:)/
    function Type() {},false,
    function String() {
        [native code]
    },false,test
  • 相关阅读:
    十层框架
    大规模web服务开发技术
    ASP.NET三层架构基础详细操作图文教程
    ASP.NET MVC4中的异步控制器
    集成多个子系统的单点登录(网站入口方式)附源码
    我的C#全能Excel操作(无需Office,不使用XML)
    代码重构——程序员应有的基因
    通过监听Windows消息对复合控件进行整体控制
    Android游戏框架
    Ext.NET
  • 原文地址:https://www.cnblogs.com/RachelChen/p/5420936.html
Copyright © 2011-2022 走看看