The Object.create() method creates a new object based on an existing object as its prototype.
// Create an Object: const person = { firstName: “John”, lastName: “Doe” }; // Create new Object const man = Object.create(person); man.firstName = “Peter”; |
The Object.keys() method returns an array containing the keys of an object.
// Create an Object const person = { firstName: “John”, lastName: “Doe”, age: 50, eyeColor: “blue” }; // Get the Keys const keys = Object.keys(person); |
ES5 introduced new methods for managing objects in JavaScript.
// Adding or changing an object property Object.defineProperty(object, property, descriptor) // Adding or changing object properties Object.defineProperties(object, descriptors) // Accessing a Property Object.getOwnPropertyDescriptor(object, property) // Accessing Properties Object.getOwnPropertyDescriptors(object) // Returns all properties as an array Object.getOwnPropertyNames(object) // Accessing the prototype Object.getPrototypeOf(object) |
ES5 introduced methods for object protection in JavaScript.
// Prevents adding properties to an object Object.preventExtensions(object) // Returns true if properties can be added to an object Object.isExtensible(object) // Prevents changes of object properties (not values) Object.seal(object) // Returns true if object is sealed Object.isSealed(object) // Prevents any changes to an object Object.freeze(object) // Returns true if object is frozen Object.isFrozen(object) |
The bind() method allows an object to borrow a method from another object.
In this example, two objects are created: person and member. The member object borrows the fullname method from the person object.
const person = { firstName:“John”, lastName: “Doe”, fullName: function () { return this.firstName + ” “ + this.lastName; } } const member = { firstName:“Hege”, lastName: “Nilsen”, } let fullName = person.fullName.bind(member); |
ES5 permits trailing commas in object and array definitions.
person = { firstName: “John”, lastName: ” Doe”, age: 46, } |
points = [ 1, 5, 10, 25, 40, 100, ]; |
// Allowed: var person = ‘{“firstName”:”John”, “lastName”:”Doe”, “age”:46}’ JSON.parse(person) // Not allowed: var person = ‘{“firstName”:”John”, “lastName”:”Doe”, “age”:46,}’ JSON.parse(person) |
// Allowed: points = [40, 100, 1, 5, 25, 10] // Not allowed: points = [40, 100, 1, 5, 25, 10,] |