Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么我需要为扩展函数设置构造函数?_Javascript_Constructor_Extend - Fatal编程技术网

Javascript 为什么我需要为扩展函数设置构造函数?

Javascript 为什么我需要为扩展函数设置构造函数?,javascript,constructor,extend,Javascript,Constructor,Extend,我试图完全理解“扩展”在javascript中是如何工作的 这是我在google上找到的一个简单的扩展函数 function extend(child, parent) { var f = function() {} f.prototype = parent.prototype; var i; for( i in parent.prototype ) { child.prototype[i] = parent.prototype[i];

我试图完全理解“扩展”在javascript中是如何工作的

这是我在google上找到的一个简单的扩展函数

function extend(child, parent) {

    var f = function() {}
    f.prototype = parent.prototype;


    var i;

    for( i in parent.prototype ) {
        child.prototype[i] = parent.prototype[i];
    }


    child.prototype.constructor = child;
    child.parent = parent;

}
它可以工作,但我不理解“child.prototype.constructor=child”部分。没有它,该函数仍然可以工作


行的目的是什么?

在我看来,“child.prototype.constructor”是对象的基本/普通实现,它允许其他对象从它扩展而不继承相同的父对象。因此,为什么要在“child.parent=parent”之前声明它。

这个示例演示了如何在javascript中继承类

var a = function(){
    // class a constructor
    this.a_priviligiate_var = 1;
}
a.prototype.a_public_var = 2;
var b = function(){
    // class b constructor
    // super call of class a constructor
    // this line makes b class to inherit all privileged vars
    b.prototype.constructor.call(this)
    this.b_priviligiate_var = 3;
}
b.prototype.b_public_var = 4;
b.prototype = new a();
var c = new b();
定义claas:

var a = function(){
    // class a constructor
    this.a_priviligiate_var = 1;
}
a.prototype.a_public_var = 2;
定义另一个类:

var b = function(){
    // class b constructor
    // super call of class a constructor
    // this line makes b class to inherit all privileged vars
    b.prototype.constructor.call(this)
    this.b_priviligiate_var = 3;
}
b.prototype.b_public_var = 4;
设置超级(父)类。这一行复制了所有的b

b.prototype = new a();
创建c的实例

var c = new b();

这可能无法直接回答您的问题,但我建议您使用John Resig的
extend
实现:

它允许您创建名为
init
的构造函数,如下所示:

var MyClass = Class.extend({
    init: function() {
         console.log("Running a constructor");
    }
});
并实例化如下对象(正常):


不,你在哪里找到的?它混合了两种方法:

经典原型继承 此函数创建一个新对象,该对象直接从父函数的原型对象继承。这是一个古老的同义词。这样,就建立了一个原型链,
child
的所有实例也从
parent.prototype
继承。由于生成一个新对象以覆盖
子.prototype
,因此

mixin遗传 此函数只是循环父对象的原型对象的所有属性,并将它们复制到子对象的原型对象上。这正是普通助手函数
extend
所做的。它不会重置子函数的“prototype”属性,但也不会设置继承链

function extend(child, parent) {
    for (var i in parent.prototype ) {
        child.prototype[i] = parent.prototype[i];
    }
}
你说得对,这条线

child.prototype.constructor = child;
在这里非常无用-“构造函数”属性不可枚举,并且不会受到扩展的影响

此外,您的函数将子函数对象的“父”属性设置为父函数,这不是必需的:

child.parent = parent;

你说的“适当”是什么意思?你的例子是什么?Javascript是一种原型语言,所以谈论类是完全错误的……而且,这些例子也是错误的
b.call(this)
就足够了,而不是创建新的实例(包括属性)
对象。应该使用create(a.prototype)
,在覆盖整个对象之前设置
b\u public\u var
,并且(最后但并非最不重要的)没有类似于“私有变量”的东西@Christoph-这是一个关于js中类的长对话,讨论它太长了here@cuzzea:有特权方法可以访问构造函数范围中的本地(“私有”)变量。原型上的方法只能访问公共属性。这似乎是错误的。您确定它不是
child.prototype=new f而不是那个循环?@Bergi//实际上我对它做了一点修改。是的,有child.prototype=新的f;我将ti更改为for(在parent.prototype中为I)。。相反它仍然不能解释我为什么需要做“child.prototype.constructor=child”,那么你为什么要这样做呢?此更改使被质疑的行无效。否<代码>构造函数
只是一个属性,仅此而已。“从它扩展而不继承同一个父项”是什么意思?//我不确定你是否还在看这篇文章,但我有一个问题。基于inherit()代码,多重继承将不起作用。你知道如何使它适用于多重继承吗?我什么都想不出来,只能混进去。
child.prototype.constructor = child;
child.parent = parent;