如何在JavaScript中使用对象的方法
介绍
JavaScript 中,使用对象是对象的方 键/值 对的集合。值可以包含属性和方法,使用并且可以包含所有其他 JavaScript 数据类型,对象的方例如字符串,使用数字和布尔值。对象的方
JavaScript中的使用所有对象都来自父 Object 的构造函数。Object 为我们提供了很多实用的对象的方内置方法,并且可以在单个对象中直接使用。使用不同于 数组的对象的方原型方法 例如 sort() 和 reverse() 只能被数组实例使用,对象方法直接来自 Object 构造函数,使用并使用对象实例作为参数。对象的方这称为静态方法。使用
本教程将介绍重要的对象的方内置对象方法,下面的使用每个部分都涉及特定方法并提供使用示例。网站模板
前提
为了充分利用本教程,您应该熟悉创建,修改和使用对象,您可以在“ 了解JavaScript中的对象 ”一文中查看这些对象。
有关JavaScript的其他指导,您可以查看“ JavaScript 如何编码 ”系列。
Object.create()
Object.create() 方法用于创建一个新对象,并将其链接到现有的对象原型。
我们可以创建一个job对象实例,并将其扩展为更具体的对象。
// Initialize an object with properties and methods const job = { position: cashier, type: hourly, isAvailable: true, showDetails() { const accepting = this.isAvailable ? is accepting applications : "is not currently accepting applications"; console.log(`The ${ this.position} position is ${ this.type} and ${ accepting}.`); } }; // Use Object.create to pass properties const barista = Object.create(job); barista.position = "barista"; barista.showDetails(); Output The barista position is hourly and is accepting applications.barista 对象现在有一个 position 属性 - 但是所有其他属性和方法都可以通过 job 的原型获得。通过Object.create()来实现最小化重复,对于保持代码DRY十分有效。
Object.keys()
Object.keys() 会创建一个包含对象键的数组。
我们可以创建一个对象并打印键的数组。
// Initialize an object const employees = { boss: Michael, secretary: Pam, sales: Jim, accountant: Oscar }; // Get the keys of the object const keys = Object.keys(employees); console.log(keys); Output ["boss", "secretary", "sales", "accountant"]Object.keys() 还可用于迭代对象的键和值。
// Iterate through the keys Object.keys(employees).forEach(key => { let value = employees[key]; console.log(`${ key}: ${ value}`); }); Output boss: Michael secretary: Pam sales: Jim accountant: Oscarfor-in 循环和Object.keys()返回的可枚举属性有一个区别:
for-in 循环同时也会遍历原型属性
Object.keys() 只会返回自有(实例)属性
Object.keys() 对于检查对象的长度也很有用。
// Get the length of the keys const length = Object.keys(employees).length; console.log(length); Output 4使用该 length 属性,源码库我们能够计算employees包含4个自有属性。
Object.values()
Object.values() 创建一个包含对象值的数组。
// Initialize an object const session = { id: 1, time: `26-July-2018`, device: mobile, browser: Chrome }; // Get all values of the object const values = Object.values(session); console.log(values); Output [1, "26-July-2018", "mobile", "Chrome"]Object.keys()和Object.values()允许您从对象返回数据。
Object.entries()
Object.entries() 创建对象的键/值对的嵌套数组。
// Initialize an object const operatingSystem = { name: Ubuntu, version: 18.04, license: Open Source }; // Get the object key/value pairs const entries = Object.entries(operatingSystem); console.log(entries); Output [ ["name", "Ubuntu"] ["version", 18.04] ["license", "Open Source"] ]一旦我们有了键/值对数组,我们就可以使用该forEach()方法循环并处理结果。
// Loop through the results entries.forEach(entry => { const [key, value] = entry; console.log(`${ key}: ${ value}`); }); Output name: Ubuntu version: 18.04 license: Open SourceObject.entries() 方法仅返回对象实例自己的属性,而不返回可通过其原型继承的任何属性。
Object.assign()
Object.assign() 用于把一个对象的值复制到另一个对象。
我们可以创建两个对象,使用Object.assign()方法将它们合并。
// Initialize an object const name = { firstName: Philip, lastName: Fry }; // Initialize another object const details = { job: Delivery Boy, employer: Planet Express }; // Merge the objects const character = Object.assign(name, details); console.log(character); Output { firstName: "Philip", lastName: "Fry", job: "Delivery Boy", employer: "Planet Express"}也可以使用展开语法(Spread syntax)来完成相同的任务。在下面的代码中,我们将通过展开语法合并name和details对象,来声明character对象。
// Initialize an object const name = { firstName: Philip, lastName: Fry }; // Initialize another object const details = { job: Delivery Boy, employer: Planet Express }; // Merge the object with the spread operator const character = { ...name, ...details} console.log(character); Output { firstName: "Philip", lastName: "Fry", job: "Delivery Boy", employer: "Planet Express"}展开语法(Spread syntax) 在对象语法中也成为浅层克隆(shallow-cloning)。
Object.freeze()
Object.freeze() 防止修改对象的属性和值,并防止在对象中添加或删除属性。
// Initialize an object const user = { username: AzureDiamond, password: hunter2 }; // Freeze the object const newUser = Object.freeze(user); newUser.password =