
JavaScript Basics: Object.create
In an earlier post (Object Creation and Manipulation Functions in JavaScript), I listed some functions that are available on the Object constructor. Here is the list once again:
create
assign
defineProperty
defineProperties
getPrototypeOf
setPrototypeOf
getOwnPropertyDescriptor
getOwnPropertyDescriptors
getOwnPropertyNames
getOwnPropertySymbols
is
seal
freeze
preventExtensions
isExtensible
isSealed
isFrozen
keys
entries
values
And also these methods are on Object.prototype:
hasOwnProperty
isPrototypeOf
propertyIsEnumerable
These functions are used for object creation and manipulation. In the current post, I am going to talk about the first one of these functions, Object.create.
Every constructor function in JavaScript has an associated prototype that is used as the prototype for the instances created by that constructor. In fact, the inheritance pattern in JavaScript is based on prototypes, so you can just create an object based on a prototype, without the need to define a constructor function. That's when Object.create becomes useful.
So Object.create creates an object based on a prototype. The prototype is specified as the first argument to this function. You can also specify a second argument that is a map of property names and property descriptors.
Here are some examples:
let prototype = {
name: "Ali",
greet() {
console.log("Hi. My name is " + this.name);
},
};
let person1 = Object.create(prototype);
let person2 = Object.create(prototype, {
age: {
value: 19,
writable: true,
enumerable: true,
configurable: true,
},
});
As you see in the example, the second argument follows the same pattern used in Object.defineProperties.
Related Posts
- Object Creation and Manipulation Functions in JavaScript
- Property Descriptors in JavaScript
- Getting and Setting Property Descriptors in JavaScript (Part 1)
- Getting and Setting Property Descriptors in JavaScript (Part 2)
- Getting and Setting Property Descriptors in JavaScript (Part 3)
- Default Values of Property Descriptors in JavaScript
Leave JavaScript Basics: Object.create to:
Read more #javascript posts
Best Posts From ghasemkiani
We have not curated any of ghasemkiani's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From ghasemkiani
- Merging PDF Files with Java
- Happy Iranian New Year
- Stephen Hawking, one of the greatest physicists of our time, dies at 76
- Using Scoped NPM Packages
- Optimistic Typing in Nashorn
- JavaScript Basics: Reflect.apply
- JavaScript Basics: Reflect.construct
- JavaScript Basics: Reflect
- New Horizons Broke the Record of the “Pale Blue Dot” Picture
- JavaScript Basics: Object.freeze and Object.isFrozen
- JavaScript Basics: Object.seal and Object.isSealed
- JavaScript Basics: Object.preventExtensions and Object.isExtensible
- JavaScript Basics: Object.prototype.hasOwnProperty
- JavaScript Basics: Object.is
- JavaScript Basics: Object.keys, Object.values, and Object.entries
- What Is Success and How Do You Define It?
- Steemit Reached 700,000 Members
- JavaScript Basics: Object.getPrototypeOf and Object.setPrototypeOf
- JavaScript Basics: Object.assign
- JavaScript Basics: Object.create