gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:06:51 24 4
gpt4 key购买 nike

假设我有一个名为 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com