如何专门化 Javascript 类?

发布于 2024-12-18 09:12:27 字数 1837 浏览 1 评论 0原文

我正在研究 JavaScript 类继承(我正在使用 node.js)。我得到子类实例的“未定义”值。这是我的示例:

我定义了一个 Squirrel 类,并且我想在 KillerSquirrel 子类中专门化该类。我想创建 Squirrel 和 KillerSquirrel 类的实例。

function Squirrel(name, color) {
        this.name = name;
        this.color = color;
        console.log("A new " + this.color + " squirrel named " + this.name + " is born!!!");
    };

    // add a new method called speak()
    Squirrel.prototype.speak = function(text) {
        console.log(this.name + " squirrel says: '" + text + "'");
    };

    // specialize the Squirrel class by creating a new KillerSquirrel constructor 
    function KillerSquirrel(name, color) {    
      this.soul = 'very bad';
      console.log("Bhrrr ... a new " + this.color + " killer squirrel named " + this.name + " is born!!!");
    }  
    KillerSquirrel.prototype.__proto__ = Squirrel.prototype;

    // add kill method  
    KillerSquirrel.prototype.kill = function(obj){  
      console.log(this.name + " squirrel killed " + obj.name + " squirrel"); 
    }

    // replace the speak() method  
    KillerSquirrel.prototype.speak = function(text) {
                       console.log(this.name + " squirrel says: 'Grhhh! " + text + "' Grhhh!");
    };

    var squirrel = new Squirrel("Gummy", "brown");
    squirrel.speak("My name is " + squirrel.name);
    var killer = new KillerSquirrel("Mr.Hide", "black");
    killer.speak("My name is " + killer.name);

我使用其构造函数创建了 Squirrel 的 squirrel 实例,并将一些值传递给构造函数,这按预期工作。当我尝试使用其构造函数创建子类 KillerSquirrel 的实例并向其传递一些值时,杀手松鼠实例具有“未定义的属性”。

看:

$ node killersquirrel.js
A new brown squirrel named Gummy is born!!!
Gummy squirrel says: 'My name is Gummy'
Bhrrr ... a new undefined killer squirrel named undefined is born!!!
undefined squirrel says: 'Grhhh! My name is undefined' Grhhh!

I was playing with JavaScript class inheritance (I am using node.js). I get "undefined" values for instances of the child class. Here my example:

I define a Squirrel class and I want to specialize this class in a KillerSquirrel child class. I want create instances of both the Squirrel and the KillerSquirrel classes.

function Squirrel(name, color) {
        this.name = name;
        this.color = color;
        console.log("A new " + this.color + " squirrel named " + this.name + " is born!!!");
    };

    // add a new method called speak()
    Squirrel.prototype.speak = function(text) {
        console.log(this.name + " squirrel says: '" + text + "'");
    };

    // specialize the Squirrel class by creating a new KillerSquirrel constructor 
    function KillerSquirrel(name, color) {    
      this.soul = 'very bad';
      console.log("Bhrrr ... a new " + this.color + " killer squirrel named " + this.name + " is born!!!");
    }  
    KillerSquirrel.prototype.__proto__ = Squirrel.prototype;

    // add kill method  
    KillerSquirrel.prototype.kill = function(obj){  
      console.log(this.name + " squirrel killed " + obj.name + " squirrel"); 
    }

    // replace the speak() method  
    KillerSquirrel.prototype.speak = function(text) {
                       console.log(this.name + " squirrel says: 'Grhhh! " + text + "' Grhhh!");
    };

    var squirrel = new Squirrel("Gummy", "brown");
    squirrel.speak("My name is " + squirrel.name);
    var killer = new KillerSquirrel("Mr.Hide", "black");
    killer.speak("My name is " + killer.name);

I create a squirrel instance of Squirrel using its constructor and passing some values to the constructor and this works as expected. When I try to create an instance of child class KillerSquirrel using its constructor and passing some values to it, the killer squirrel instance has "undefined properties".

see:

$ node killersquirrel.js
A new brown squirrel named Gummy is born!!!
Gummy squirrel says: 'My name is Gummy'
Bhrrr ... a new undefined killer squirrel named undefined is born!!!
undefined squirrel says: 'Grhhh! My name is undefined' Grhhh!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

椒妓 2024-12-25 09:12:27

子类构造函数应该通过特殊构造(apply 或 call)手动调用超类构造函数,如下所示:

function KillerSquirrel(name, color) {
  Squirrel.apply(this, arguments);    
  this.soul = 'very bad';
  console.log("Bhrrr ... a new " + this.color + " killer squirrel named " + this.name + " is born!!!");
}  

或者

function KillerSquirrel(name, color) {
  Squirrel.call(this, name, color);    
  this.soul = 'very bad';
  console.log("Bhrrr ... a new " + this.color + " killer squirrel named " + this.name + " is born!!!");
}  

尽管对于这种情况(当参数相同时),第一种形式是首选。

Subclass constructor should call superclass constructor manually by special construct (apply or call), like this:

function KillerSquirrel(name, color) {
  Squirrel.apply(this, arguments);    
  this.soul = 'very bad';
  console.log("Bhrrr ... a new " + this.color + " killer squirrel named " + this.name + " is born!!!");
}  

or

function KillerSquirrel(name, color) {
  Squirrel.call(this, name, color);    
  this.soul = 'very bad';
  console.log("Bhrrr ... a new " + this.color + " killer squirrel named " + this.name + " is born!!!");
}  

though for this case (when the arguments are the same) the first form is preferred.

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