是否可以在 JavaScript 中仅使用 apply 函数而不使用原型属性来使用继承?

发布于 2024-11-09 15:55:12 字数 1206 浏览 3 评论 0原文

我正在尝试学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

耳根太软 2024-11-16 15:55:12

它之所以有效,是因为您将 Book 构造函数中的函数添加到 this 中。因此,无论您通过 apply 传递给 Book 的任何对象,方法都会在该对象上设置。

但是 TechBook 没有 Book 通过其原型可能拥有的方法。

例如,如果您有类似的内容:

Book.prototype.getTitle = function()...

那么除非您正确设置 TechBook 的原型,否则 TechBook 将不会有此功能。


通过在构造函数中设置函数,您可以为您创建的每个实例创建每个方法的新实例。原型用于避免这种情况,以便多个实例可以共享代码。

It works because you are adding the functions in the Book constructor to this. So whatever object you pass to Book via apply, the methods are set on this object.

But TechBook does not have the methods Book might have through its prototype.

E.g. if you had something like:

Book.prototype.getTitle = function()...

then TechBook would not have this function unless you set TechBook'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.

很快妥协 2024-11-16 15:55:12

我正在尝试学习 JavaScript 对象
遗产。我指的是
JavaScript Cookbook,作者:Shelley Powers。

在子类中你需要调用
superclass.apply(this,arguments) 到
使用它的属性。并根据
我还需要写的书
类似于 subclass.prototype =
新的超类();

我没有那本书。 Javascript 没有类,最好暂时忘记它们,只关注原型继承的工作原理。您可以模拟基于类的继承,但这通常是不必要的,甚至是不需要的。

您的代码不使用原型继承,它在构造函数创建的每个实例上创建属性。

当您从 TechBook 中调用 Book 时,它不会作为构造函数调用,而是作为函数调用并从 this 对象传递code>TechBook,因此属性会直接添加到该对象。

new TechBook 返回的对象继承自 TechBook.prototype,但您尚未为其设置任何属性。

代码模式似乎使用构造函数来使用闭包来模拟私有和特权成员,模块模式更常用于此目的。

最后,BookTechBook 更适合编写为函数声明而不是表达式。

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();

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 within TechBook, it isn't called as a constructor, it's called as a function and passed the this object from TechBook, 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文