Objects in Javascript

Objects in Javascript

Objects in javascript are really different from other programming languages. Simple types in javascript are number,string,boolean, null and undefined all other values are object.Even arrays and functions in javascript are objects.Numbers,Strings and Booleans are object-like as they have methods but they immutable.Let me explain with code examples,

var x = 9.656;

x.toExponential(2);

console.log(x); // 9.656

You see the value of x remains unchanged. This is immutability in javascript. Same for the remaining 2.

An object is a container of properties, where a property has a name and a value. A property name can be any string, including the empty string. A property value can be any JavaScript value except for undefined.

Defining an object using object Literal:

var new_empty_object = {};

var person = {

     “firstName”: “Muhammad”,

       “secondName”:”Akif”

};

Property Retrieval:

An Object property value can be retrieved using [ ] suffix with property name in string.It can also be retrieved directly by name if it is a legal js name and not a reserved word.

console.log(person[“firstName”]); // Muhammad

console.log(person.secondName); //Akif

Pass by value and Pass by reference:

Objects in javascript are not pass by value. Objects are always passed by reference so they are mutable in javascript.

var a = {};var b = {};var c = {}; // all pointing to different memory locations

var a = b = c = {}; // all pointing to same memory location.

Prototype:

Every object is linked to a prototype object from which it can inherit properties. All objects created from object literal( {} ) are linked to Object.prototype, an object that comes standard with JavaScript.When you make a new object, you can select the object that should be its prototype. The mechanism that JavaScript provides to do this is messy and complex, but it can be significantly simplified.

let have an example

if (typeof Object.newObject!== ‘function’) {

      Object.newObject= function (o) {

      var F = function () {}; //it behaves like class in javascript(in es 5 now we can use directly class keyword);

      F.prototype = o;// setting class prototype;

      return new F();// return a new object;

   };

}

var another_a= Object.newObject(stooge);

Prototype is not used for updating properties but only for retrieval of properties.Let me explain briefly about prototyping when we retrieve a property from a an object it looks in its property if found then returned if not it looks in the object in its prototype and so on until Object.prototype is reached.If the desired property exists nowhere in the prototype chain, then the result is the undefined value. This is called delegation.

Reflections:

What are reflections ? Actually reflections are used to obtain information about variables object etc at runtime. It help us in type checking and many other things.

typeof person.firstName // ‘number’

typeof person.lastName // ‘string’

typeof person // ‘object’

typeof person.xyz // ‘undefined’

Delete:

The delete operator can be used to remove a property from an object. It will remove a property from the object if it has one. It will not touch any of the objects in the prototype linkage. 

Enumeration:

We can loop through object properties using for in loop but it will not ensure the order of properties. The enumeration will include all of the properties including functions and prototype properties that you might not be interested in.so it is necessary to filter out the values you don’t want. The most common filters are the hasOwnProperty method and using type to exclude functions. It also includes properties from the prototype.

var firstObject = {name:”a”};
var secondObject = Object.newObject(firstObject);
secondObject .age = 30;
for(let obj in secondObject ){
console.log(secondObject [obj]);
}
output:
“a”
30

order not ensured.

I hope its helpful. Best of Luck!

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *