The properties and values are stored in a key-value pair.
new keyword. Example
- Numbers can be made object using
- Boolean can be made object using
- Strings can be made object using
- Array is an object
- Function is an object
- Date is an object
- Regex (Regular Expression) is an object
- Maths is an object
- Objects are always objects
- Creating object using object literals
- Creating object with constructor
- Creating object With object.create() method
1. Creating object using object literals
Object is created using object literal by assigning curly braces to the variable name.
Here is how to create an empty object.
Property is added to the object in key-value pairs, value could be any data type.Now, here is how to add a
key : value pair in the object.
Here, the object person has three properties:
2. Creating object with constructor
Here is how to create an object using a constructor function.
3. Creating object With object.create() method
The Object.create() method is used to create object using older objects as a prototype. Using this method all the properties that are created referring to older objects, has to be given value in new object
object method is a function that operates on an object.
object method is part of the object in the form of a key value pair.
To access object method write method name followed by object name connected with dot. Here is an example:
Check Working example.
this keyword indicates the parent object of the function.
this referes to the object which is holding the function.
In the above example
this indicates the money object. In remaining function this.earning = money.earning, this.spent = money.spent and so on.