Introduction
Number, String, Boolean, null, and undefined are simple types of JavaScript, these simple types have methods (object-like) but they are immutable. All other values in JavaScript are objects.
JavaScript is an object-oriented language but JavaScript objects are different than other languages.
- var car =
- {
- brand: 'Audi6',
- color: 'White'
- };
- function Car(brand, color)
- {
- this.brand = brand,
- this.color = color
- }
- var car = new Car('Ferrari', 'Red');
In the above statement we are just adding the properties to this. Here it's important to understand this (referencing alias) keyword which is again an object. That object is whatever object is executing the current bit of code. By default that is the global object in web browse; it is a window object.
New keyword here creates an empty JavaScript function that sets the context of this to a new object.
Object.Create():
This is the what is actually happening down the wire when we createan object using using object literals and constructor functions.
- var car = Object.create(Object.prototype,
- { brand : {value :'Mercedes', enumerable:true, writable:true, configurable:true},
- color : {value :'Black', enumerable:true, writable:true, configurable:true},
- })
ECMA6 Classes: For browsers supporting ECMA6Script features. Looks like similar to static language classes.
Example:
Advance work with Object Properties Attributes:
- class Car
- {
- constructor(brand, color)
- {
- this.brand = brand,
- this.color = color
- }
- honk()
- {
- alert('POooo')
- }
- }
- var car = new Car('Tesla', 'Blue');
- Object.defineProperty(car, 'brand' , {writable: false})
- cat.brand = 'XYZ' // throw error in strict mode.
Enumerable: Define that can we loop over the property using a for ..in loop.
Configurable:
Lock down property from being changed also prevent the property from being deleted from the object.
- Object.defineProperty(car, fullDetail)
- {
- get: function()
- {
- return this.brand + ' ' + this.color
- },
- set: function(value)
- {
- var nameParts = value.split(' ')
- this.brand = nameParts[0]
- this.color = nameParts[1]
- }
- })

Join the conversation! Your thoughts help the community grow.