````
var cat = {
name: "Gus",
color: "gray",
age: 15,
printInfo: function() {
console.log("Name:", this.name, "Color:", this.color, "Age:", this.age);
nestedFunction = function() {
console.log("Name:", this.name, "Color:", this.color, "Age:", this.age)
}
nestedFunction();
}
}
cat.printInfo()
````
//打印:名称:Gus颜色:灰色年龄:15-由printInfo打印 // PRINTS:名称:undefined颜色:undefined年龄:undefined-由nestedFunction打印 ``` 班级信息{ 构造函数(名称,颜色,年龄){ this.name =名称 this.color =颜色 这个年龄=年龄 } printInfo = function(){ console.log(“ Name:”,this.name,“ Color:”,this.color,“ Age:”,this.age); nestedFunction = function(){ console.log(“ Name:”,this.name,“ Color:”,this.color,“ Age:”,this.age); } nestedFunction(); } } var obj = new info(“ thomas”,“ orange”,“ 26”) obj.printInfo() `` //打印:名称:托马斯颜色:橙色年龄:26(由printInfo打印) ReferenceError:未定义nestedFunction(此后将引发错误) 在第一种情况下,直接对象是制成的,在第二种情况下,对象是使用class制成的。 但是输出是不同的 在类对象中,由于未定义nestedFunction,因此会引发错误 请告诉我直接对象和使用类模板制作的对象之间的区别。