Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.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_Inheritance_Prototype_Extend - Fatal编程技术网

JavaScript继承问题

JavaScript继承问题,javascript,inheritance,prototype,extend,Javascript,Inheritance,Prototype,Extend,这是《Pro JavaScript设计模式》一书中的扩展函数 我对前三行有问题。。。它创建一个空函数,然后将F.prototype设置为superClass.prototype,这意味着(对于两个新的构造函数函数,例如foo、bar和foo extends bar),F.prototype将有一个构造函数属性:bar和proto:Object,还是没有? 第3行:subClass.prototype=new F();发生了一些我无法理解的事情。。当F的[[Prototype]]是对象时,为什么会

这是《Pro JavaScript设计模式》一书中的扩展函数

我对前三行有问题。。。它创建一个空函数,然后将F.prototype设置为superClass.prototype,这意味着(对于两个新的构造函数函数,例如foo、bar和foo extends bar),F.prototype将有一个构造函数属性:bar和proto:Object,还是没有? 第3行:subClass.prototype=new F();发生了一些我无法理解的事情。。当F的[[Prototype]]是对象时,为什么会发生继承


前三行和后三行之间的区别是什么

subClass.prototype = new superClass();
代码何时执行?我的意思是第一个和第二个做的是一样的


只需添加一个对子类中的超类构造函数的调用。
调用是“className”.superclass.constructor.call(this)

我们有施工人员:

F
Super
Sub

我们有对象
f
super
,&
sub
,它们是使用上述构造函数生成的

i、 e.
f=new f
super=new super
sub=new sub

我们从第2行知道
f.\uuuuu proto\uuuu===super.\uuuu proto\uuuu==super.prototype

从第3行我们可以看到
sub.\uuuuu proto\uuuu==f
sub.\uuuu proto\uuuu==Super.prototype

我们还有第5行的
Sub.superClass===Super.prototype

从第4行和第6行我们可以说
sub.constructor===sub
&
super.constructor===super

我们之所以可以在第7行之前的第3行调用
new F
before,是因为第7行设置了

f.uuu proto\uuuu.constructor===Super
其中as
f.constructor
已经是
Sub
。基本上,第7行正在清理
Super
并且根本不应该影响
Sub
,因为您不应该在实际代码中调用
。\uuuuuu proto\uuuu.constructor

此特定函数明确性不将
Super
称为函数,而是确保通过
Sub
构造的对象在其链中具有
Super.prototype

function extend(subClass, superClass) {

    // create a new empty function
    var F = function() {}; 

    // set the prototype of that function to prototype object of the superclass
    F.prototype = superClass.prototype;

    // now set the prototype of the subClass to a new instance of F
    // this is done so that subClass.prototype.foo = does not affect
    // superClass.prototype.foo
    // if you don't do this, changes to one subClass will affect superClass and all
    // other subClasses of superClass
    subClass.prototype = new F();
注意:
subClass.prototype=new superClass()
将调用构造函数并使用返回的新对象作为原型。这将导致
超类
本身的属性出现在原型链上(因为该实例就是原型对象)


作为一种选择,您可以考虑原型JavaScript库。它包括继承

下面是原型文档中的一个示例

var Animal = Class.create({
  initialize: function(name, sound) {
    this.name  = name;
    this.sound = sound;
  },

  speak: function() {
    alert(this.name + " says: " + this.sound + "!");
  }
});

// subclassing Animal
var Snake = Class.create(Animal, {
  initialize: function($super, name) {
    $super(name, 'hissssssssss');
  }
});

var ringneck = new Snake("Ringneck");
ringneck.speak();
//-> alerts "Ringneck says: hissssssssss!"

var rattlesnake = new Snake("Rattler");
rattlesnake.speak();
//-> alerts "Rattler says: hissssssssss!"

事实上,第3行的子部分“proto”是foo,Chrome console是这么说的。。。这正是困扰我的地方。@bliof看到所有的断言都像我说的那样成立。@Raynos据我所知,当搜索一个属性时,它从当前对象开始,如果它不在那里,它就会转到[[Prototype]],但在这里子类的[[Prototype]]之间没有连接(或者我看不到连接)和超类。@bliof
sup.\uuuuu proto\uuuuu===Super.prototype
。和
sub.\uuuu proto\uuu===sub.prototype
在chain@RaynosSuper.prototype是默认对象,它不是超类,是吗?您可以找到关于上一个的解释。10倍的评论。@Bilah,好的。在“扩展”函数中仍然没有什么意义,我不希望修改超类。好吧,这样做是为了确保子类中的构造函数调用能够工作。嘿,Ivo——我看到了这个线程,我很好奇(几年后),如果最后一点更有意义的话。我个人还没有完全理解它,不知道是否有更多的理解:if(superClass.prototype.constructor==Object.prototype.constructor),以及为什么要重置为superClass。什么时候会覆盖超类构造函数?
    // set the constructor, this indicates that 'subClass' is a function
    subClass.prototype.constructor = subClass;

    // add a property to indicate what the superClass is
    // this way one can call super methods via this.superclass.something
    subClass.superclass = superClass.prototype;

    // in case the superClass was an object...
    if(superClass.prototype.constructor == Object.prototype.constructor) {

       // change the constructor?
       // no idea why they do this here
       // this introduces a side effect to the function
       superClass.prototype.constructor = superClass;
    }
}
var Animal = Class.create({
  initialize: function(name, sound) {
    this.name  = name;
    this.sound = sound;
  },

  speak: function() {
    alert(this.name + " says: " + this.sound + "!");
  }
});

// subclassing Animal
var Snake = Class.create(Animal, {
  initialize: function($super, name) {
    $super(name, 'hissssssssss');
  }
});

var ringneck = new Snake("Ringneck");
ringneck.speak();
//-> alerts "Ringneck says: hissssssssss!"

var rattlesnake = new Snake("Rattler");
rattlesnake.speak();
//-> alerts "Rattler says: hissssssssss!"