JavaScript Object Methods Example.
- Object.keys(obj): Returns an array of an object's own enumerable property names (keys).
const obj = { a: 1, b: 2, c: 3 }; console.log(Object.keys(obj)); // Output: ['a', 'b', 'c']
- Object.values(obj): Returns an array of the object's own enumerable property values.
const obj = { a: 1, b: 2, c: 3 }; console.log(Object.values(obj)); // Output: [1, 2, 3]
- Object.entries(obj): Returns an array of the object's own enumerable string-keyed property [key, value] pairs.
const obj = { a: 1, b: 2, c: 3 }; console.log(Object.entries(obj)); // Output: [['a', 1], ['b', 2], ['c', 3]]
- Object.isSealed(obj): Returns true if the object is sealed, otherwise false.
const obj = Object.seal({ a: 1 }); console.log(Object.isSealed(obj)); // Output: true
- Object.assign(target, source): Copies the values of all enumerable properties from one or more source objects to a target object. It returns the target object.
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 }
- Object.freeze(obj): Freezes an object, preventing new properties from being added or existing properties from being removed or reconfigured.
const obj = { name: 'Khabib' }; Object.freeze(obj); obj.name = 'Bob'; // This won't change the value console.log(obj.name); // Output: 'Khabib'
- Object.seal(obj): Seals an object, preventing new properties from being added, but allowing existing properties to be modified.
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' }
- Object.create(proto): Creates a new object with the specified prototype object and properties.
const person = {greet() {console.log('Hello!');}}; const student = Object.create(person); student.greet(); // Output: 'Hello!'
- Object.defineProperty(obj, prop, descriptor): Defines a new property directly on an object or modifies an existing property.
const obj = {}; Object.defineProperty(obj, 'name', { value: 'Alice', writable: false }); console.log(obj.name); // 'Alice'
- Object.defineProperties(obj, props): Defines multiple new properties or modifies existing properties on an object.
const obj = {}; Object.defineProperties(obj, { name: { value: 'Cormier', writable: false }, age: { value: 30, writable: true } }); console.log(obj.name); // 'Cormier'
- Object.isExtensible(obj): Determines if an object is extensible (i.e., whether new properties can be added).
const obj = {}; console.log(Object.isExtensible(obj)); // true Object.preventExtensions(obj); console.log(Object.isExtensible(obj)); // false
- Object.isFrozen(obj): Determines if an object is frozen (i.e., not extensible and all properties are non-writable).
const obj = Object.freeze({ name: 'Gregor' }); console.log(Object.isFrozen(obj)); // output: true
- Object.hasOwn(obj, prop): Returns true if the specified object has the specified property as its own property, even if the property's value is undefined.
const obj = { name: 'Alice' }; console.log(Object.hasOwn(obj, 'name')); // true console.log(Object.hasOwn(obj, 'age')); // false
- Object.hasOwnProperty(prop): Determines if an object contains the specified property as a direct property of that object and not inherited through the prototype chain.
const obj = { name: 'Alice' }; console.log(obj.hasOwnProperty('name')); // true console.log(obj.hasOwnProperty('age')); // false
- Object.preventExtensions(obj): Prevents new properties from ever being added to an object.
const obj = {}; Object.preventExtensions(obj); obj.name = 'Khabib'; // Won't be added console.log(obj); // {}
- Object.setPrototypeOf(obj, proto): Sets the prototype (the internal [[Prototype]] property) of a specified object.
const proto = { greet() {console.log('Hello!');}}; const obj = {}; Object.setPrototypeOf(obj, proto); obj.greet(); // 'Hello!'
- Object.fromEntries(iterable): Transforms a list of key-value pairs into an object.
const obj = { a: 1, b: 2, c: 3 }; console.log(Object.keys(obj)); // Output: ['a', 'b', 'c']
- Object.getPrototypeOf(obj): Returns the prototype (the internal [[Prototype]] property) of the specified object.
const obj = { a: 1, b: 2, c: 3 }; console.log(Object.values(obj)); // Output: [1, 2, 3]
- Object.getOwnPropertySymbols(obj): Returns an array of all symbol properties found on the object.
const obj = { a: 1, b: 2, c: 3 }; console.log(Object.entries(obj)); // Output: [['a', 1], ['b', 2], ['c', 3]]
- Object.getOwnPropertyDescriptor(obj, prop): Returns a property descriptor for a specific property of a given object.
const obj = Object.seal({ a: 1 }); console.log(Object.isSealed(obj)); // Output: true
- Object.getOwnPropertyNames(obj): Returns an array of all properties found on the object (including non-enumerable properties).
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 }
- Object.is(value1, value2): Compares if two values are the same.
const obj = { name: 'Khabib' }; Object.freeze(obj); obj.name = 'Bob'; // This won't change the value console.log(obj.name); // Output: 'Khabib'
- Object.getOwnPropertyDescriptors(obj): Returns all own property descriptors of an object.
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!

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
