Thursday, December 3, 2015

JavaScript: Prototype Object Vs Reference Object Explained

What is an Object?
It is a set of key and value pairs. You can use any number of keys with any name as long as it is a String. And each key can be associated with any value, those values can be of any Types, such as: Primitive Types, Function Objects and object itself.
var myObject = {
  a: undefined, //Primitive
  b: null, //Primitive
  c: true, //Primitive
  d: "foo", //Primitive
  e: 4.33, //Primitive
  f: function bar() { /**/ }, //Object
  g: {
    h: baz
  } //Object
}

The difference:

In case of Objects - values are passed by reference, and the changes are bi-directional:
var a = { x: 1, y: 2}
var b = a;
console.log(b); // Object {x: 1, y: 2} 

//
b.x = 10;
console.log(a.x); // 10

//
a.y = 20;
console.log(b.y); //20

While in Primitive - the values are passed by value, and the changes are uni-directional:
//Prototype Object
var object1 = {x: 11, y: 22};
var object2 = Object.create(object1);
console.log(object2); //Object {}

//
object2.x = 100; 
console.log(object1.x); //11 
console.log(object2.x); //100 

//
object1.y = 200; 
console.log(object2.y); //200 
console.log(object1.y); //200 

Fiddle: http://jsfiddle.net/sL6q0742/



17 comments:

  1. Really you have done a good job. Thanks for sharing this valuable information....
    Appium Training in Chennai
    Appium Online Training

    ReplyDelete
  2. event management. Doubling down on becoming the leading networking-focused hybrid event platform and They continue to build an impressive client list that includes some of the largest companies in B2B events. presenter biography template and thank you message for organising event

    ReplyDelete
  3. This post is so interactive and informative.keep updating more information...
    aws training in mumbai
    aws training in kochi
    aws training in trivandrum

    ReplyDelete
  4. Excellent data with lots of information. I have bookmarked this page for my future reference. Do share more updates.
    Salesforce Training In Chennai
    Salesforce Course In Chennai
    Salesforce Training Online

    ReplyDelete
  5. This post is so interactive and informative.keep updating more information...
    AWS Exams
    AWS Architecture Certification

    ReplyDelete
  6. Great content with proper explanation. I've some doubts but now all is clear. Thanks for explaining it perfectly.

    While building process-driven applications use of monolithic architecture will affect the development process and also limit the development due to the complexity. To increase the agility of process-driven applications, developers must go for a flexible architecture.
    Micro frontend architecture changing the approach of developers towards web application architectures.

    Micro Frontend Architecture

    ReplyDelete