JavaScript Objects And Property Attributes

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 in JavaScript objects are different than other languages.

"JavaScript object is a dynamic collection of properties".

Objects in JavaScript are mutable keyed collections. A property is a key value pair where using key string we can get the value.

How to create properties on JavaScript

Object literal:
This is the simplest way of creating objects, we define the properties and their values inside braces. 

Example:
  1. var car =  
  2. {  
  3. brand: 'Audi6',  
  4. color: 'White'  
  5. };  
Because of the dynamic nature of Objects in JavaScript we can add the property into an object(car) at any time i.e. car.year = 1984. No need to go and changing data type, no need to recompile.

Constructor Function:


For a scenario where we need to create multiple instances of an object, JavaScript does not have classes like static languages. This is being achieved by using new keywords followed by a function to create object.

Example:
  1. function Car(brand, color)  
  2. {  
  3. this.brand = brand,  
  4. this.color = color  
  5. }  
  6. 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 global object in web browse; it is window object.

New keyword here creates a empty JavaScript function that set the context of this to 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.
  1. var car = Object.create(Object.prototype,  
  2. { brand : {value :'Mercedes', enumerable:true, writable:true, configurable:true},  
  3. color : {value :'Black', enumerable:true, writable:true, configurable:true},  
  4. })  
ECMA6 Classes: For browsers supporting ECMA6Script features. Looks like similar to static language classes.

Example:
  1. class Car  
  2. {  
  3. constructor(brand, color)  
  4. {  
  5. this.brand = brand,  
  6. this.color = color  
  7. }  
  8. honk()  
  9. {  
  10. alert('POooo')  
  11. }  
  12. }  
  13. var car = new Car('Tesla''Blue');  
Advance work with Object Properties Attributes:

In JavaScript we have writable, enumerable, configurable property attributes which can do pretty cool things. Highlighted above while creating property using Object.Create()

Writable:
Define when the object property value can be changed from its initial value.
  1. Object.defineProperty(car, 'brand' , {writable: false})  
  2. 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 a property from being changed also prevent the property from being deleted from the object.

Getter and Setters:
Attribute on a property that allow us to specify the return value of a property using function ans set the value of a property using a function.

Example:
  1. Object.defineProperty(car, fullDetail)  
  2. {  
  3. get: function()  
  4. {  
  5. return this.brand + ' ' + this.color  
  6. },  
  7. set: function(value)  
  8. {  
  9. var nameParts = value.split(' ')  
  10. this.brand = nameParts[0]  
  11. this.color = nameParts[1]  
  12. }  
  13. })  
Ebook Download
View all
Learn
View all