我有这个用 OOP 编写的简单代码块。我想操纵这句话,以便将最后一个参数 ( faveColor
) 更改为指定的实际颜色。我知道我需要编写某种函数来做到这一点,但我不知道怎么做。
我真的很困惑如何以最干净的方式解决这个问题。基本上这样句子就会输出“你好,我的名字是 John Doe,我最喜欢的颜色是 ${red/orange/blue/green} 等。每个选项的颜色都会变化。
function Person(fullName, faveColour) {
this.name = fullName;
this.favouriteColour = faveColour;
this.greet = function () {
return `Hello, my name is ${this.name} and my favourite colour is ${this.favouriteColour}`;
};
}
const john = new Person('John Doe', 'red');
john.greet();
const jane = new Person('Jane Doe', 'orange');
jane.greet();
const red = '#ff0000';
const orange = '#ffa500';