javascript - 如何在 javascript 的类中用对象包装函数?

标签 javascript function oop object javascript-objects

假设我有一个名为 Human 的类。

function Human(name, gender, age, personality){
    this.name = name;
    this.gender = gender;
    this.age = age;
    this.personality = personality; 
}

而且我有一些类的功能,例如greetingintroduce。所以我这样创建它们:

Human.prototype.introduce = function() {
    var _gender = {"M": "Boy", "F": "Girl"};
    switch(this.personality)
    {
    case "passionate":
        alert("Hi, how are you? My name is " + this.name + ". I'm " + this.age + " years old " + _gender[this.gender] + ". ");
        break;
    case "aggressive":
        alert("I'm " + this.name + ". What you want? ");
        break;
    }
}

Human.prototype.greeting = function() {
    alert("Hi!");
}

既然introducegreeting 可以归为同一个类别(让我们将其命名为speak),我怎样才能简单地包装这两个函数与一个对象?我试过这个:

Human.prototype.speak = {};
Human.prototype.speak.greeting = function(){
    alert("Hi!");
}
Human.prototype.speak.introduce = function(){
var _gender = {"M": "Boy", "F": "Girl"};
        switch(this.personality)
        {
        case "passionate":
            alert("Hi, how are you? My name is " + this.name + ". I'm " + this.age + " years old " + _gender[this.gender] + ". ");
            break;
        case "aggressive":
            alert("I'm " + this.name + ". What you want? ");
            break;
        }
}

现在的问题是,当一个函数被一个对象包裹时,introduce 函数中的this 不再引用该实例。那么我该如何解决这个问题呢?

我想这样调用这个函数:

var eminem = new Human("Marshall Mathers", "M", 45, "aggressive");
eminem.speak.introduce();

最佳答案

类演讲

Speak 成为一个类,因为 OOP 中的变量和功能的逻辑分组是类。

说话

function Speak(human) {
    this.human = human
}

Speak.prototype.greeting = function () {
    // ...
}

Speak.prototype.introduce = function () {
    // ..
}

人类

function Human(name, gender, age, personality, greetWord) {
    this.name = name;
    this.gender = gender;
    this.age = age;
    this.personality = personality;
    this.speak  = new Speak(this)
}

例子

function Human(name, gender, age, personality, greetWord) {
    this.name = name;
    this.gender = gender;
    this.age = age;
    this.personality = personality;
    this.greetWord = greetWord;
    this.speak  = new Speak(this)
}

function Speak(human) {
    this.human = human
}

Speak.prototype.greeting = function () {
    alert(this.human.greetWord + "!");
}

Speak.prototype.introduce = function () {
    var _gender = { "M": "Boy", "F": "Girl" };
    switch (this.human.personality) {
        case "passionate":
            alert("Hi, how are you? My name is " + this.human.name + ". I'm " + this.human.age + " years old " + _gender[this.human.gender] + ". ");
            break;
        case "aggressive":
            alert("I'm " + this.human.name + ". What you want? ");
            break;
    }
}

var peter = new Human('Peter', 'M', 35, 'aggressive', 'Hi')
peter.speak.greeting()
peter.speak.introduce()


解决方案的另一种方法是使用应用。

Apply

The apply() method calls a function with a given this value, and arguments provided as an array (or an array-like object).

代码

var peter = new Human('Peter', 'M', 35, 'aggressive')
console.log(peter.speak.introduce.apply(peter))

例子

function Human(name, gender, age, personality){
    this.name = name;
    this.gender = gender;
    this.age = age;
    this.personality = personality; 
}

Human.prototype.speak = {};
Human.prototype.speak.greeting = function(){
    alert("Hi!");
}
Human.prototype.speak.introduce = function(){
var _gender = {"M": "Boy", "F": "Girl"};
        switch(this.personality)
        {
        case "passionate":
            alert("Hi, how are you? My name is " + this.name + ". I'm " + this.age + " years old " + _gender[this.gender] + ". ");
            break;
        case "aggressive":
            alert("I'm " + this.name + ". What you want? ");
            break;
        }
}

var peter = new Human('Peter', 'M', 35, 'aggressive')
console.log(peter.speak.introduce.apply(peter))

关于javascript - 如何在 javascript 的类中用对象包装函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47280342/

相关文章:

Javascript 101 - 类型转换变量传递给函数

python - 在Python中部署azure函数不会安装requirements.txt中的依赖项

c++ - 为什么通过公开继承另一个类来构建一个类是一种糟糕的做法?

javascript - 使用 ASP.NET 和 C# 将文件上传到服务器

javascript - 获取周数但每周五更改为下一周的脚本

c# - 从抽象类和非抽象类派生的区别

java - 为什么这个方法不使用对象的任何属性?

javascript - 如何从下拉列表中选择的值填充 javascript 数组?

javascript - 获取用户可见的所有 html 控件?

javascript - 以编程方式编译模板并附加到元素