是否可以在 JavaScript 中仅使用 apply 函数而不使用原型属性来使用继承?
我正在尝试学习 JavaScript 对象继承。我指的是 Shelley Powers 的《JavaScript Cookbook》。
在子类中,您需要调用 superclass.apply(this,arguments) 来使用其属性。根据这本书,我还需要编写类似 subclass.prototype = new superclass(); 的内容
但是,我注意到无需使用 subclass.prototype = new superclass(); 即可工作。陈述。下面是我的代码。 subclass.prototype = new superclass(); 的目的是什么?
var Book = function (newTitle, newAuthor) {
var title;
var author;
title = newTitle;
author = newAuthor;
this.getTitle = function () {
return title;
}
this.getAuthor = function () {
return author;
}
};
var TechBook = function (newTitle, newAuthor, newPublisher) {
var publisher = newPublisher;
this.getPublisher = function () {
return publisher;
}
Book.apply(this, arguments);
this.getAllProperties = function () {
return this.getTitle() + ", " + this.getAuthor() + ", " + this.getPublisher();
}
}
//TechBook.prototype = new Book(); // Even when commented,
var b1 = new TechBook("C Pro", "Alice", "ABC Publishing");
var b2 = new TechBook("D Pro", "Bob", "DEF Publishing");
alert(b1.getAllProperties());
alert(b2.getAllProperties());
alert(b1.getTitle());
alert(b2.getTitle());
I am trying to learn JavaScript object inheritance. I am referring to JavaScript Cookbook by Shelley Powers.
In the subclass you need to call superclass.apply(this,arguments) to use its properties. And according to the book I also need to write something like subclass.prototype = new superclass();
However, I noted that things work without using the subclass.prototype = new superclass(); statement. Below is my code. What is the purpose of subclass.prototype = new superclass();
var Book = function (newTitle, newAuthor) {
var title;
var author;
title = newTitle;
author = newAuthor;
this.getTitle = function () {
return title;
}
this.getAuthor = function () {
return author;
}
};
var TechBook = function (newTitle, newAuthor, newPublisher) {
var publisher = newPublisher;
this.getPublisher = function () {
return publisher;
}
Book.apply(this, arguments);
this.getAllProperties = function () {
return this.getTitle() + ", " + this.getAuthor() + ", " + this.getPublisher();
}
}
//TechBook.prototype = new Book(); // Even when commented,
var b1 = new TechBook("C Pro", "Alice", "ABC Publishing");
var b2 = new TechBook("D Pro", "Bob", "DEF Publishing");
alert(b1.getAllProperties());
alert(b2.getAllProperties());
alert(b1.getTitle());
alert(b2.getTitle());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它之所以有效,是因为您将
Book
构造函数中的函数添加到this
中。因此,无论您通过apply
传递给Book
的任何对象,方法都会在该对象上设置。但是
TechBook
没有Book
通过其原型可能拥有的方法。例如,如果您有类似的内容:
那么除非您正确设置
TechBook
的原型,否则TechBook
将不会有此功能。通过在构造函数中设置函数,您可以为您创建的每个实例创建每个方法的新实例。原型用于避免这种情况,以便多个实例可以共享代码。
It works because you are adding the functions in the
Book
constructor tothis
. So whatever object you pass toBook
viaapply
, the methods are set on this object.But
TechBook
does not have the methodsBook
might have through its prototype.E.g. if you had something like:
then
TechBook
would not have this function unless you setTechBook
's prototype correctly.By setting the functions in the constructor, you create new instances of each method for every instance you create. A prototype is used to avoid that, so that several instances can share code.
我没有那本书。 Javascript 没有类,最好暂时忘记它们,只关注原型继承的工作原理。您可以模拟基于类的继承,但这通常是不必要的,甚至是不需要的。
您的代码不使用原型继承,它在构造函数创建的每个实例上创建属性。
当您从
TechBook
中调用Book
时,它不会作为构造函数调用,而是作为函数调用并从this
对象传递code>TechBook,因此属性会直接添加到该对象。new TechBook
返回的对象继承自 TechBook.prototype,但您尚未为其设置任何属性。代码模式似乎使用构造函数来使用闭包来模拟私有和特权成员,模块模式更常用于此目的。
最后,Book 和 TechBook 更适合编写为函数声明而不是表达式。
I don't have that book. Javascript doesn't have classes, best to forget about them for the time being and just concentrate on how prototype inheritance works. You can emulate class-based inheritance, but it is usually not necessary or even desirable.
Your code doesn't use prototype inheritance, it creates properties on each instance created by the constructors.
When you call
Book
from withinTechBook
, it isn't called as a constructor, it's called as a function and passed thethis
object fromTechBook
, so properties are added directly to that object.The object returned by
new TechBook
inherits from TechBook.prototype, but you haven't set any properties on it.The code pattern seems to be using constructors to emulate private and privileged members using closures, the module pattern is more commonly used for that.
Lastly, Book and TechBook would be more appropriately be written as function declarations rather than expressions.