gpt4 book ai didi

javascript - 一个数组中的对象字面量和对象构造函数是否存在潜在问题?

转载 作者:行者123 更新时间:2023-12-03 02:25:44 24 4
gpt4 key购买 nike

我想深入了解 JavaScript 中的对象创建。一般来说,我了解创建方法之间的差异等。当我使用下面两种方式创建数组中的对象时,您能描述一下这两种情况下使用对象数组的区别和影响吗?

function Animal(name, species) {
this.name = name,
this.species = species
}

var animals = [
{name: 'azor', species: 'dog'},
{name: 'mruczek', species: 'cat'},
{name: 'burek', species: 'dog'},
];

//Case 1
newAnimal = {name: 'motek', species: 'buterfly'}
animals.push(newAnimal);
console.log(animals);

console.log('---');

//Case 2
newAnimal = new Animal('bongo', 'elephant');
animals.push(newAnimal);
console.log(animals);

我问是因为如果我使用控制台它会显示

[ { name: 'azor', species: 'dog' },
{ name: 'mruczek', species: 'cat' },
{ name: 'burek', species: 'dog' },
{ name: 'motek', species: 'buterfly' } ]
---
[ { name: 'azor', species: 'dog' },
{ name: 'mruczek', species: 'cat' },
{ name: 'burek', species: 'dog' },
{ name: 'motek', species: 'buterfly' },
Animal { name: 'bongo', species: 'elephant' } ]

问题

//Case 2一样,在同一个数组中使用文字和构造函数创建对象是否有问题?

最佳答案

如果您希望所有对象都真正成为 Animal 实例,您可以使用 Object.setPrototypeOf()重新定义对象的原型(prototype):

class Animal {
constructor (name, species) {
this.name = name;
this.species = species;
}

greet () {
return `My name is ${this.name} and I am a ${this.species}`;
}
}

var animals = [
{name: 'azor', species: 'dog'},
{name: 'mruczek', species: 'cat'},
{name: 'burek', species: 'dog'},
];

animals.push({name: 'motek', species: 'buterfly'});
animals.push(new Animal('bongo', 'elephant'));

animals.forEach(animal => {
Object.setPrototypeOf(animal, Animal.prototype)
});

console.log(`Are all the animals actually an Animal? ${
animals.every(animal => animal instanceof Animal)
}`);
console.log(animals.map(animal => animal.greet()));

或者您可以使用多态性形式将所有对象视为动物,方法是使用 Function.prototype.call() :

class Animal {
constructor (name, species) {
this.name = name;
this.species = species;
}

greet () {
return `My name is ${this.name} and I am a ${this.species}`;
}
}

var animals = [
{name: 'azor', species: 'dog'},
{name: 'mruczek', species: 'cat'},
{name: 'burek', species: 'dog'},
];

animals.push({name: 'motek', species: 'buterfly'});
animals.push(new Animal('bongo', 'elephant'));

// animals.forEach(animal => {
// Object.setPrototypeOf(animal, Animal.prototype)
// });

console.log(`Are all the animals actually an Animal? ${
animals.every(animal => animal instanceof Animal)
}`);
console.log(animals.map(animal => Animal.prototype.greet.call(animal)));

请注意,在这种情况下,它们仍然是最初定义的,但您可以调用 Animalprototype 方法,并将每个实例作为调用上下文传递。

哦,console.log() 输出不同的原因是 [Symbol.toStringTag]属性,您可以重载:

var newAnimal = {name: 'motek', species: 'buterfly', [Symbol.toStringTag]: 'Animal'}
console.log(newAnimal);
// Animal {name: "motek", species: "buterfly", Symbol(Symbol.toStringTag): "Animal"}

关于javascript - 一个数组中的对象字面量和对象构造函数是否存在潜在问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48960403/

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