相关疑难解决方法(0)

使用`Object.create`进行继承的好处

我一直试图围绕Object.createECMAScript 5中引入的新方法.

通常,当我想使用继承时,我会这样做:

var Animal = function(name) { this.name = name; }
Animal.prototype.print = function() { console.log(this.name); }

var Dog = function() 
{ 
  return Animal.call(this, 'Dog'); 
}

Dog.prototype = new Animal();
Dog.prototype.bark = function() { console.log('bark'); }
Run Code Online (Sandbox Code Playgroud)

我只是将一个新创建的Animal对象分配给Dog的原型,一切都像魅力:

var dog1 = new Dog();
dog1.print(); // prints 'Dog'
dog1.bark(); // prints 'bark'
dog1.name; //prints 'Dog'
Run Code Online (Sandbox Code Playgroud)

但人们(没有解释)说这Dog.prototype = new Animal();不是继承的工作方式,我应该使用Object.create方法:

Dog.prototype = Object.create(Animal.prototype);
Run Code Online (Sandbox Code Playgroud)

这也有效.

使用的好处是什么,Object.create或者我错过了什么?

更新:有人说这Dog.prototype = Animal.prototype;也可以.所以现在我完全糊涂了

javascript

52
推荐指数
4
解决办法
4779
查看次数

使用prototype /"new"继承

嗨,我是Javascript OO的新手,想要了解有关继承的更多信息.希望你能提供一些建议!

我看到这篇精彩文章: 如何在JavaScript中"正确"创建自定义对象?

正如我在其他网站上看到的那样谈论一个类是如何继承的,例如:

function man(x) {
    this.x = x;
    this.y = 2;
}
man.prototype.name = "man";
man.prototype.two = function() {
    this.y = "two";
}
function shawn() {
    man.apply(this, arguments);
};
shawn.prototype = new man;
Run Code Online (Sandbox Code Playgroud)

上面的帖子声称,为了在继承时不调用"man"的构造函数,可以使用这样的帮助器:

function subclassOf(base) {
    _subclassOf.prototype= base.prototype;
    return new _subclassOf();
}
function _subclassOf() {};
shawn.prototype = subclassOf(man);
Run Code Online (Sandbox Code Playgroud)

虽然我理解它的意图,但我不明白为什么我们不能打电话

shawn.prototype = man.prototype;
Run Code Online (Sandbox Code Playgroud)

我觉得它的作用完全一样.还是有什么我想念的?提前致谢!

javascript oop

5
推荐指数
2
解决办法
2867
查看次数

javascript继承中正确的原型做法是什么?

我已经阅读了几篇关于js继承的文章(这一篇,这一篇,这一篇,等等)

在Mozilla的这篇文章中,"经典"继承如下所示:(我统一的例子)

// inherit Base
function Derived() { ... }
Derived.prototype = new Base();            <-------
Derived.prototype.constructor = Derived;   <-------
Run Code Online (Sandbox Code Playgroud)

但是在本文中我看到:

// inherit Base
function Derived() { ... }
Derived.prototype = Object.create(Base.prototype);    <-------
Derived.prototype.constructor = Derived;
Run Code Online (Sandbox Code Playgroud)

而且我也看到了这个:

Derived.prototype = Base.prototype;
Run Code Online (Sandbox Code Playgroud)

而且我也进行了实验,但未找到constructor矫揉造作的用法:

Derived.prototype.constructor = Derived; <--- it still work if I skip this line
Run Code Online (Sandbox Code Playgroud)

如果我跳过此行,无论如何都要new Derived()正确调用Derived().

那么1)什么是正确的:

  1. Derived.prototype = new Base();
  2. Derived.prototype = Object.create(Base.prototype);
  3. Derived.prototype = …

javascript inheritance

5
推荐指数
1
解决办法
146
查看次数

对于使用JavaScript的继承,引用或复制父级原型是否更好?

目前,我在我的JavaScript库中实现了继承,如下所示:

parent = function() { };
parent.prototype.hello = function() { alert('parent'); };

child = function() { };
child.prototype = parent.prototype;
Run Code Online (Sandbox Code Playgroud)

但是,我注意到当我覆盖子类"class"原型中的函数时,它也会不合理地覆盖父类的原型:

child.prototype.hello = function() { alert('child'); }

(new parent()).hello();  // alerts "child" --> undesirable
(new child()).hello();   // alerts "child" --> desirable
Run Code Online (Sandbox Code Playgroud)

如果我从孩子的原型中删除了一些东西

delete child.prototype.hello;
Run Code Online (Sandbox Code Playgroud)

然后父母的原型受到不利影响.也许吧

child.prototype = parent.prototype;
Run Code Online (Sandbox Code Playgroud)

是不是实现继承的最佳方式?而不是让孩子"类"引用父原型,也许将父原型复制到子原型更有意义?

_.extend(child.prototype, parent.prototype);
Run Code Online (Sandbox Code Playgroud)

javascript inheritance prototype

4
推荐指数
1
解决办法
366
查看次数

标签 统计

javascript ×4

inheritance ×2

oop ×1

prototype ×1