- 使用括号表示法将 name 属性的值存储在变量 catName 中。
- 使用点表示法运行 greeting() 方法。
- 将color 属性值更新为 白。
- 重写 greeting() 方法,使它的问候语为 "孟买猫 碳头 对你说:你好。",请注意 孟买猫 和 碳头
是变量。
- 编写自己的 cat2 对象 ,它具有和 cat 相同的结构、完全相同的方法,但具有不同的 name
breed color。
- 定义两次 cat 违反了编程中“不要重复自己”的原则,请用构造函数来创建实例 cat3 ,使其只需定义一次。
const cat = {
name : "碳头",
breed : "孟买猫",
color : "黑",
greeting: function(breed,name) {
console.log(`${cat.breed} ${![]()cat.name} 对你说: 你好。`);
}
}
const catName=cat["name"];
cat.greeting();
cat["color"]='白';
const cat2 = {
name : "咪咪",
breed : "矮脚猫",
color : "白",
greeting: function(breed,name) {
console.log(`${cat2.breed} ${![]()cat2.name} 对你说: 你好。`);
}
}
function cat3(name,breed,color){
![]()this.name=name;
this.breed=breed;
this.color=color;
this.greeting=function (){
console.log(`${this.breed} ${![]()this.name} 对你说: 你好。`);
}
}
console.log(`这只猫的名字叫 ${ catName }。`);
console.log(`这只猫的颜色是 ${ cat.color }。`);
cat.greeting();
cat2.greeting();
const ca=new cat3("碳头","孟买猫","黑");
ca.greeting();