了解 JavaScript 中的關鍵物件方法
JavaScript’s Object comes packed with a number of useful methods that help developers manipulate objects with ease. Let’s walk through some of the most important ones, with brief explanations and examples
- Object.create()
- Object.assign()
- Object.keys()
- Object.values()
- Object.entries()
- Object.freeze()
- Object.seal()
- Object.preventExtensions()
- Object.getPrototypeOf()
- Object.setPrototypeOf()
- Object.defineProperty()
- Object.defineProperties()
- Object.getOwnPropertyDescriptor()
- Object.getOwnPropertyDescriptors()
- Object.getOwnPropertyNames()
- Object.is()
- Object.isFrozen()
- Object.isSealed()
- Object.isExtensible()
- Object.fromEntries()
- Object.hasOwnProperty()
- Object.hasOwn()
- Object.groupBy() (proposed feature, may not be fully available)
Object.create()
Object.create() is a method in JavaScript used to create a new object with a specified prototype object and optional properties. It allows for more fine-grained control over an object's prototype and properties compared to using object literals or constructors.
const personPrototype = { greet() { console.log(`Hello, my name is ${this.name}`); } }; const john = Object.create(personPrototype); john.name = "John"; john.greet(); // Output: Hello, my name is John
Object.assign()
Object.assign() is a built-in JavaScript method used to copy the values of all enumerable own properties from one or more source objects to a target object. It performs a shallow copy and returns the modified 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 } console.log(target); // Output: { a: 1, b: 2, c: 3 } (target is also modified)
Object.keys()
Returns an array of the 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()
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()
Returns an array of the object’s own enumerable 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.freeze()
Freezes the object, preventing new properties from being added or existing properties from being changed or deleted
const obj = { a: 1 }; Object.freeze(obj); obj.a = 2; // No effect, because the object is frozen console.log(obj.a); // Output: 1
Object.seal()
Seals the object, preventing new properties from being added, but allows existing properties to be modified.
const obj = { a: 1 }; Object.seal(obj); obj.a = 2; // Allowed delete obj.a; // Not allowed console.log(obj.a); // Output: 2
Object.preventExtensions()
Prevents any new properties from being added to the object, but allows modification and deletion of existing properties
const obj = { a: 1 }; Object.preventExtensions(obj); obj.b = 2; // Not allowed console.log(obj.b); // Output: undefined
Object.getPrototypeOf()
Returns the prototype (i.e., the internal [[Prototype]]) of the specified object
const obj = {}; const proto = Object.getPrototypeOf(obj); console.log(proto); // Output: {} (the default Object prototype)
Object.setPrototypeOf()
Sets the prototype of a specified object.
const proto = { greet() { console.log('Hello!'); } }; const obj = {}; Object.setPrototypeOf(obj, proto); obj.greet(); // Output: 'Hello!'
Object.defineProperty()
Defines a new property on an object or modifies an existing one, with additional options for property descriptors (e.g., writable, configurable).
const obj = {}; Object.defineProperty(obj, 'a', { value: 42, writable: false, // Cannot modify the value }); obj.a = 100; // No effect because writable is false console.log(obj.a); // Output: 42
Object.defineProperties()
Defines multiple properties on an object with property descriptors.
const obj = {}; Object.defineProperties(obj, { a: { value: 42, writable: false }, b: { value: 100, writable: true } }); console.log(obj.a); // Output: 42 console.log(obj.b); // Output: 100
Object.getOwnPropertyDescriptor()
Returns the descriptor for a property of an object.
const obj = { a: 1 }; const descriptor = Object.getOwnPropertyDescriptor(obj, 'a'); console.log(descriptor); // Output: { value: 1, writable: true, enumerable: true, configurable: true }
Object.getOwnPropertyDescriptors()
Returns an object containing all property descriptors for an object’s own properties
const obj = { a: 1 }; const descriptors = Object.getOwnPropertyDescriptors(obj); console.log(descriptors); // Output: { a: { value: 1, writable: true, enumerable: true, configurable: true } }
Object.getOwnPropertyNames()
Returns an array of all properties (including non-enumerable ones) found directly on an object.
const obj = { a: 1 }; Object.defineProperty(obj, 'b', { value: 2, enumerable: false }); console.log(Object.getOwnPropertyNames(obj)); // Output: ['a', 'b']
Object.is()
Compares if two values are the same (like === but handles special cases like NaN)
console.log(Object.is(NaN, NaN)); // Output: true console.log(Object.is(+0, -0)); // Output: false
Object.isFrozen()
Checks if an object is frozen
const obj = Object.freeze({ a: 1 }); console.log(Object.isFrozen(obj)); // Output: true
Object.isSealed()
Checks if an object is sealed.
const obj = Object.seal({ a: 1 }); console.log(Object.isSealed(obj)); // Output: true
Object.isExtensible()
Checks if new properties can be added to an object.
const obj = { a: 1 }; Object.preventExtensions(obj); console.log(Object.isExtensible(obj)); // Output: false
Object.fromEntries()
Converts an array of key-value pairs into an object
const entries = [['a', 1], ['b', 2]]; const obj = Object.fromEntries(entries); console.log(obj); // Output: { a: 1, b: 2 }
Object.hasOwnProperty()
Checks if an object has the specified property as its own (not inherited)
const obj = { a: 1 }; console.log(obj.hasOwnProperty('a')); // Output: true
Object.hasOwn()
Object.hasOwn() is a newer method introduced in ES2022 as an alternative to Object.hasOwnProperty(). It checks whether an object has a direct (own) property with a specified key, without looking up the prototype chain.
const obj = { name: 'Alice', age: 25 }; console.log(Object.hasOwn(obj, 'name')); // true console.log(Object.hasOwn(obj, 'gender')); // false
Object.groupBy
Object.groupBy is a relatively new feature proposed for JavaScript in ECMAScript 2024 that allows you to group objects based on a common criterion. It is not yet widely available across all environments, so it may not work in many browsers or JavaScript engines until fully implemented.
const array = [ { name: 'Alice', age: 25 }, { name: 'Bob', age: 30 }, { name: 'Charlie', age: 25 }, { name: 'David', age: 30 }, ]; // Group objects by age const groupedByAge = Object.groupBy(array, item => item.age); console.log(groupedByAge); /* Expected Output: { 25: [ { name: 'Alice', age: 25 }, { name: 'Charlie', age: 25 } ], 30: [ { name: 'Bob', age: 30 }, { name: 'David', age: 30 } ] } */
以上是了解 JavaScript 中的關鍵物件方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Python更適合初學者,學習曲線平緩,語法簡潔;JavaScript適合前端開發,學習曲線較陡,語法靈活。 1.Python語法直觀,適用於數據科學和後端開發。 2.JavaScript靈活,廣泛用於前端和服務器端編程。

JavaScript在Web開發中的主要用途包括客戶端交互、表單驗證和異步通信。 1)通過DOM操作實現動態內容更新和用戶交互;2)在用戶提交數據前進行客戶端驗證,提高用戶體驗;3)通過AJAX技術實現與服務器的無刷新通信。

JavaScript在現實世界中的應用包括前端和後端開發。 1)通過構建TODO列表應用展示前端應用,涉及DOM操作和事件處理。 2)通過Node.js和Express構建RESTfulAPI展示後端應用。

理解JavaScript引擎內部工作原理對開發者重要,因為它能幫助編寫更高效的代碼並理解性能瓶頸和優化策略。 1)引擎的工作流程包括解析、編譯和執行三個階段;2)執行過程中,引擎會進行動態優化,如內聯緩存和隱藏類;3)最佳實踐包括避免全局變量、優化循環、使用const和let,以及避免過度使用閉包。

Python和JavaScript在開發環境上的選擇都很重要。 1)Python的開發環境包括PyCharm、JupyterNotebook和Anaconda,適合數據科學和快速原型開發。 2)JavaScript的開發環境包括Node.js、VSCode和Webpack,適用於前端和後端開發。根據項目需求選擇合適的工具可以提高開發效率和項目成功率。

C和C 在JavaScript引擎中扮演了至关重要的角色,主要用于实现解释器和JIT编译器。1)C 用于解析JavaScript源码并生成抽象语法树。2)C 负责生成和执行字节码。3)C 实现JIT编译器,在运行时优化和编译热点代码,显著提高JavaScript的执行效率。

Python更適合數據科學和自動化,JavaScript更適合前端和全棧開發。 1.Python在數據科學和機器學習中表現出色,使用NumPy、Pandas等庫進行數據處理和建模。 2.Python在自動化和腳本編寫方面簡潔高效。 3.JavaScript在前端開發中不可或缺,用於構建動態網頁和單頁面應用。 4.JavaScript通過Node.js在後端開發中發揮作用,支持全棧開發。

JavaScript在網站、移動應用、桌面應用和服務器端編程中均有廣泛應用。 1)在網站開發中,JavaScript與HTML、CSS一起操作DOM,實現動態效果,並支持如jQuery、React等框架。 2)通過ReactNative和Ionic,JavaScript用於開發跨平台移動應用。 3)Electron框架使JavaScript能構建桌面應用。 4)Node.js讓JavaScript在服務器端運行,支持高並發請求。
