zoukankan      html  css  js  c++  java
  • prototype、constructor那点事儿

    一、什么是原型(prototype)

      1、prototype本质上还是一个JavaScript对象;

      2、每个函数都有一个默认的prototype属性;

      3、通过prototype我们可以扩展Javascript的内建对象


    如果这个函数被用在创建自定义对象的场景中,我们称这个函数为构造函数。 比如:

    代码
    // 构造函数
    function Person(name) {
    this.name = name;
    }
    // 定义Person的原型,原型中的属性可以被自定义对象引用
    Person.prototype = {getName:function () {
    returnthis.name;
    }};
    var zhang =new Person("ZhangSan");
    console.log(zhang.getName());
    // "ZhangSan"

    我们可以通过prototype扩展Javascript的内建对象,例如:

    代码
    // 向JavaScript固有类型Array扩展一个获取最小值的方法
    Array.prototype.min =function () {
    var min =this[0];
    for (var i =1; i <this.length; i++) {
    if (this[i] < min) {
    min
    =this[i];
    }
    }
    return min;
    };

    // 在任意Array的实例上调用min方法
    console.log([1, 56, 34, 12].min()); // 1

    注意:这里有一个陷阱,向Array的原型中添加扩展方法后,当使用for-in循环数组时,这个扩展方法也会被循环出来。

    下面的代码说明这一点(假设已经向Array的原型中扩展了min方法):

    var arr = [1, 56, 34, 12];
    var total =0;
    for (var i in arr) {
    total
    += parseInt(arr[i], 10);
    }
    console.log(total);
    // NaN

    解决方法也很简单:

    var arr = [1, 56, 34, 12];
    var total =0;
    for (var i in arr) {
    if (arr.hasOwnProperty(i)) {
    total
    += parseInt(arr[i], 10);
    }
    }
    console.log(total);
    // 103

    二、构造器(constructor)

        1、constructor始终指向创建当前对象的构造(初始化)函数。
        2、每个函数都有一个默认的属性prototype,而这个prototype的constructor默认指向这个函数


    我们来看个例子:

    代码
    // 等价于 var arr = new Array(1, 56, 34, 12);
    var arr = [1, 56, 34, 12];
    console.log(arr.constructor
    === Array); // true
    //
    等价于 var foo = new Function();
    var Foo =function () {
    };
    console.log(Foo.constructor
    === Function); // true
    //
    由构造函数实例化一个obj对象
    var obj =new Foo();
    console.log(obj.constructor
    === Foo); // true

    // 将上面两段代码合起来,就得到下面的结论
    console.log(obj.constructor.constructor === Function); // true

    但是当constructor遇到prototype时,有趣的事情就发生了。
    我们知道每个函数都有一个默认的属性prototype,而这个prototype的constructor默认指向这个函数。如下例所示:

    代码
    function Person(name) {
    this.name = name;
    }
    Person.prototype.getName
    =function () {
    returnthis.name;
    };
    var p =new Person("ZhangSan");
    console.log(p.constructor
    === Person); // true
    console.log(Person.prototype.constructor === Person); // true
    //
    将上两行代码合并就得到如下结果
    console.log(p.constructor.prototype.constructor === Person); // true

    当时当我们重新定义函数的prototype时(注意:和上例的区别,这里不是修改而是覆盖), constructor的行为就有点奇怪了,如下示例

    代码
    function Person(name) {

    this.name = name;
    }
    Person.prototype
    = {getName:function () {
    returnthis.name;
    }};
    var p =new Person("ZhangSan");
    console.log(p.constructor
    === Person); // false
    console.log(Person.prototype.constructor === Person); // false
    console.log(p.constructor.prototype.constructor === Person); // false

    为什么呢?
    原来是因为覆盖Person.prototype时,等价于进行如下代码操作:

    Person.prototype =new Object({getName:function () {
    returnthis.name;
    }});

    而constructor始终指向创建自身的构造函数,所以此时Person.prototype.constructor === Object

    怎么修正这种问题呢?方法也很简单,重新覆盖Person.prototype.constructor即可:

    代码
    function Person(name) {
    this.name = name;
    }
    Person.prototype
    =new Object({getName:function () {
    returnthis.name;
    }});
    Person.prototype.constructor
    = Person;
    var p =new Person("ZhangSan");
    console.log(p.constructor
    === Person); // true
    console.log(Person.prototype.constructor === Person); // true
    console.log(p.constructor.prototype.constructor === Person); // true
  • 相关阅读:
    html分页自适应居中;css设置分页自适应居中
    jQuery如何给DOM添加ID
    jquery如何获取div下ul的某个li
    js正则验证数字的方法
    css改变input输入框placeholder值颜色
    js校验表单后提交表单的三种方法总结
    Thinkcmf子栏目获取父级栏目所有子栏目列表
    Thinkcmf截取内容长度
    thinkcmf2.2 火狐浏览器图片上传以及谷歌图片上传打开稍慢
    c语言结构体中的冒号的用法
  • 原文地址:https://www.cnblogs.com/onflying/p/3231860.html
Copyright © 2011-2022 走看看