开发者社区> 问答> 正文

javascript中原型对象的指向问题。。

jQuery.fn = jQuery.prototype = {
    // The current version of jQuery being used
    jquery: core_version,
    constructor: jQuery    // ??这里的疑惑
}

上面是jQuery的部分源码,我想知道为什么给jQuery的原型对象上的constructor重新指向jQuery?不重新指向的话会出现什么问题?

展开
收起
云栖技术 2016-05-25 10:56:02 1846 0
1 条回答
写回答
取消 提交回答
  • 社区爱好者,专为云栖社区服务!

    这里把jQuery.prototype重新赋值了,所以原来的jQuery.prototype.constructor会被覆盖掉。如果jQuery失去constructor的话,就不能通过constructor来构造一个同类型的新对象,也不能用来判断自己的类型。例如下面的代码:

    Test = function () {
        this.name = "hello";
    }
    
    Test.prototype = {};
    var t1 = new Test;
    var t2 = new t1.constructor;
    console.log(t1.constructor === Object); // true
    console.log(t2.name); // undefined
    2019-07-17 19:13:49
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
JavaScript面向对象的程序设计 立即下载
Delivering Javascript to World 立即下载
编程语言如何演化-以JS的private为例 立即下载