Home > Article > Web Front-end > JavaScript Object Methods Example
const obj = { a: 1, b: 2, c: 3 }; console.log(Object.keys(obj)); // Output: ['a', 'b', 'c']
const obj = { a: 1, b: 2, c: 3 }; console.log(Object.values(obj)); // Output: [1, 2, 3]
const obj = { a: 1, b: 2, c: 3 }; console.log(Object.entries(obj)); // Output: [['a', 1], ['b', 2], ['c', 3]]
const obj = Object.seal({ a: 1 }); console.log(Object.isSealed(obj)); // Output: true
const target = { a: 1 }; const source = { b: 2, c: 3 }; const result = Object.assign(target, source); console.log(result); // Output: { a: 1, b: 2, c: 3 }
const obj = { name: 'Khabib' }; Object.freeze(obj); obj.name = 'Bob'; // This won't change the value console.log(obj.name); // Output: 'Khabib'
const obj = { name: 'Alice' }; Object.seal(obj); obj.name = 'Bob'; // This will update the value obj.age = 25; // This won't add a new property console.log(obj); // Output: { name: 'Bob' }
const person = {greet() {console.log('Hello!');}}; const student = Object.create(person); student.greet(); // Output: 'Hello!'
const obj = {}; Object.defineProperty(obj, 'name', { value: 'Alice', writable: false }); console.log(obj.name); // 'Alice'
const obj = {}; Object.defineProperties(obj, { name: { value: 'Cormier', writable: false }, age: { value: 30, writable: true } }); console.log(obj.name); // 'Cormier'
const obj = {}; console.log(Object.isExtensible(obj)); // true Object.preventExtensions(obj); console.log(Object.isExtensible(obj)); // false
const obj = Object.freeze({ name: 'Gregor' }); console.log(Object.isFrozen(obj)); // output: true
const obj = { name: 'Alice' }; console.log(Object.hasOwn(obj, 'name')); // true console.log(Object.hasOwn(obj, 'age')); // false
const obj = { name: 'Alice' }; console.log(obj.hasOwnProperty('name')); // true console.log(obj.hasOwnProperty('age')); // false
const obj = {}; Object.preventExtensions(obj); obj.name = 'Khabib'; // Won't be added console.log(obj); // {}
const proto = { greet() {console.log('Hello!');}}; const obj = {}; Object.setPrototypeOf(obj, proto); obj.greet(); // 'Hello!'
const obj = { a: 1, b: 2, c: 3 }; console.log(Object.keys(obj)); // Output: ['a', 'b', 'c']
const obj = { a: 1, b: 2, c: 3 }; console.log(Object.values(obj)); // Output: [1, 2, 3]
const obj = { a: 1, b: 2, c: 3 }; console.log(Object.entries(obj)); // Output: [['a', 1], ['b', 2], ['c', 3]]
const obj = Object.seal({ a: 1 }); console.log(Object.isSealed(obj)); // Output: true
const target = { a: 1 }; const source = { b: 2, c: 3 }; const result = Object.assign(target, source); console.log(result); // Output: { a: 1, b: 2, c: 3 }
const obj = { name: 'Khabib' }; Object.freeze(obj); obj.name = 'Bob'; // This won't change the value console.log(obj.name); // Output: 'Khabib'
const obj = { name: 'Alice' }; Object.seal(obj); obj.name = 'Bob'; // This will update the value obj.age = 25; // This won't add a new property console.log(obj); // Output: { name: 'Bob' }
The above is the detailed content of JavaScript Object Methods Example. For more information, please follow other related articles on the PHP Chinese website!