用于判断一个对象是否包含特定的自身属性。 它只会返回对象自身具有的属性,而不会返回从原型链上继承的属性。
obj.hasOwnProperty(prop)
const obj = {
name: "Alice",
age: 25
};
console.log(obj.hasOwnProperty('name')); // 输出: true
console.log(obj.hasOwnProperty('toString')); // 输出: false,因为 toString 是从 Object 原型继承的
const obj = {
name: "Alice"
};
// 假设有人不小心修改了 Object.prototype
Object.prototype.customProperty = "I'm a custom property";
// 直接使用 hasOwnProperty 可能会出错,特别是如果 customProperty 被设置为不可枚举
console.log(obj.hasOwnProperty('name')); // 仍然安全,输出: true
console.log(Object.prototype.hasOwnProperty.call(obj, 'name')); // 更安全的方式,输出: true
const obj = {};
// 定义一个可枚举属性
Object.defineProperty(obj, 'enumerableProperty', {
value: 'I am enumerable',
enumerable: true,
writable: true,
configurable: true
});
// 定义一个不可枚举属性
Object.defineProperty(obj, 'nonEnumerableProperty', {
value: 'I am non-enumerable',
enumerable: false,
writable: true,
configurable: true
});
// 使用 for...in 循环遍历对象属性
for (let key in obj) {
console.log(key, obj[key]);
// 只会输出 enumerableProperty 和其值,因为 nonEnumerableProperty 是不可枚举的
}
// 使用 Object.keys() 方法获取对象的可枚举属性键
console.log(Object.keys(obj));
// 输出: [ 'enumerableProperty' ],因为 nonEnumerableProperty 是不可枚举的
// 使用 Object.getOwnPropertyDescriptor() 方法获取属性的描述对象
console.log(Object.getOwnPropertyDescriptor(obj, 'enumerableProperty'));
// 输出: { value: 'I am enumerable', writable: true, enumerable: true, configurable: true }
console.log(Object.getOwnPropertyDescriptor(obj, 'nonEnumerableProperty'));
// 输出: { value: 'I am non-enumerable', writable: true, enumerable: false, configurable: true }
getOwnPropertyNames 是 JavaScript 中的一个方法,用于返回一个由指定对象的所有自身属性的属性名(包括不可枚举属性但不包括 Symbol 值作为名称的属性)组成的数组。
Object.getOwnPropertyNames(obj)
返回一个包含所有自身属性名(字符串)的数组。
let obj = {
enumerableProperty: "I am enumerable",
nonEnumerableProperty: "I am not enumerable"
};
// 定义一个不可枚举属性
Object.defineProperty(obj, "nonEnumerableProperty", {
enumerable: false
});
console.log(Object.getOwnPropertyNames(obj));
// 输出: ["enumerableProperty", "nonEnumerableProperty"]
// 对比 Object.keys()
console.log(Object.keys(obj));
// 输出: ["enumerableProperty"]
在 JavaScript 中,Symbol 是一种特殊的原始数据类型,用于创建独一无二的标识符(也称为唯一标识符)。Symbol 属性是指使用 Symbol 值作为属性名来定义对象的属性。这种属性具有唯一性、不可变性和私有性等特点。
let sym = Symbol('description');
let obj = {};
obj[sym] = 'This is a symbol property';
console.log(obj[sym]); // 输出: This is a symbol property
console.log(obj['description']); // 输出: undefined
在这个例子中,sym 是一个 Symbol 值,它被用作 obj 对象的属性名。由于 Symbol 属性的私有性,你不能通过常规的属性访问方式(如使用字符串键)来访问这个属性。
Object.getOwnPropertyDescriptor() 是 JavaScript 中的一个静态方法,用于获取对象上给定属性的属性描述符。这个方法返回一个对象,该对象描述了指定属性的配置信息,包括它是否可枚举(enumerable)、可配置(configurable)、可写(writable)以及它的值(value)等属性。
Object.getOwnPropertyDescriptor(obj, prop)
如果对象上存在指定属性,则返回一个对象,该对象描述了该属性的配置。这个返回的对象包含以下属性之一或全部:
let obj = {
prop: 'value'
};
let descriptor = Object.getOwnPropertyDescriptor(obj, 'prop');
console.log(descriptor);
// 输出: { value: 'value', writable: true, enumerable: true, configurable: true }
let obj = {};
// 使用 Object.defineProperty() 定义一个不可枚举属性
Object.defineProperty(obj, 'nonEnumerableProp', {
value: 'This is a non-enumerable property',
enumerable: false,
writable: true,
configurable: true
});
// 使用 Object.getOwnPropertyDescriptor() 获取属性描述符
let descriptor = Object.getOwnPropertyDescriptor(obj, 'nonEnumerableProp');
console.log(descriptor);
// 输出: {
// value: 'This is a non-enumerable property',
// writable: true,
// enumerable: false,
// configurable: true
// }