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中文網其他相關文章!

不同JavaScript引擎在解析和執行JavaScript代碼時,效果會有所不同,因為每個引擎的實現原理和優化策略各有差異。 1.詞法分析:將源碼轉換為詞法單元。 2.語法分析:生成抽象語法樹。 3.優化和編譯:通過JIT編譯器生成機器碼。 4.執行:運行機器碼。 V8引擎通過即時編譯和隱藏類優化,SpiderMonkey使用類型推斷系統,導致在相同代碼上的性能表現不同。

JavaScript在現實世界中的應用包括服務器端編程、移動應用開發和物聯網控制:1.通過Node.js實現服務器端編程,適用於高並發請求處理。 2.通過ReactNative進行移動應用開發,支持跨平台部署。 3.通過Johnny-Five庫用於物聯網設備控制,適用於硬件交互。

我使用您的日常技術工具構建了功能性的多租戶SaaS應用程序(一個Edtech應用程序),您可以做同樣的事情。 首先,什麼是多租戶SaaS應用程序? 多租戶SaaS應用程序可讓您從唱歌中為多個客戶提供服務

本文展示了與許可證確保的後端的前端集成,並使用Next.js構建功能性Edtech SaaS應用程序。 前端獲取用戶權限以控制UI的可見性並確保API要求遵守角色庫

JavaScript是現代Web開發的核心語言,因其多樣性和靈活性而廣泛應用。 1)前端開發:通過DOM操作和現代框架(如React、Vue.js、Angular)構建動態網頁和單頁面應用。 2)服務器端開發:Node.js利用非阻塞I/O模型處理高並發和實時應用。 3)移動和桌面應用開發:通過ReactNative和Electron實現跨平台開發,提高開發效率。

JavaScript的最新趨勢包括TypeScript的崛起、現代框架和庫的流行以及WebAssembly的應用。未來前景涵蓋更強大的類型系統、服務器端JavaScript的發展、人工智能和機器學習的擴展以及物聯網和邊緣計算的潛力。

JavaScript是現代Web開發的基石,它的主要功能包括事件驅動編程、動態內容生成和異步編程。 1)事件驅動編程允許網頁根據用戶操作動態變化。 2)動態內容生成使得頁面內容可以根據條件調整。 3)異步編程確保用戶界面不被阻塞。 JavaScript廣泛應用於網頁交互、單頁面應用和服務器端開發,極大地提升了用戶體驗和跨平台開發的靈活性。

Python更适合数据科学和机器学习,JavaScript更适合前端和全栈开发。1.Python以简洁语法和丰富库生态著称,适用于数据分析和Web开发。2.JavaScript是前端开发核心,Node.js支持服务器端编程,适用于全栈开发。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

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

Dreamweaver Mac版
視覺化網頁開發工具