JavaScript改變屬性值的方法有以下幾種:
var person = {
name: 'John',
age: 30,
};
person.name = 'Jane';
console.log(person.name); // Output: Jane
var person = {
name: 'John',
age: 30,
};
person['name'] = 'Jane';
console.log(person['name']); // Output: Jane
var propName = 'age';
person[propName] = 35;
console.log(person[propName]); // Output: 35
var person = {
name: 'John',
};
Object.defineProperty(person, 'name', {
value: 'Jane',
writable: true, // 是否可寫
enumerable: true, // 是否可枚舉
configurable: true, // 是否可配置
});
console.log(person.name); // Output: Jane
var person = {};
Object.defineProperties(person, {
name: {
value: 'John',
writable: true,
enumerable: true,
configurable: true,
},
age: {
value: 30,
writable: true,
enumerable: true,
configurable: true,
},
});
console.log(person.name); // Output: John
console.log(person.age); // Output: 30
這些是JavaScript中常用的改變屬性值的方法,可以根據具體需求選擇合適的方法來修改對象的屬性值。