Voice of a Developer: JavaScript Objects

JavaScript is a language of the Web. This series of articles will talk about my observations learned during my decade of software development experience with JavaScript. What mistakes a developer generally maes and what differences they should be aware of.

In part 1, we learnt about Data Types, and touched on Objects. But part 2 will focus in detail on Objects. You can go through Part 1 from here,

Voice of a Developer: JavaScript Data Types

Q: Can you use save asa  reference in Javascript like we have in other programming languages?

Ans:

Yes, let’s go back in C / C++ days where we have a concept of pointers,

ex:

int x=49;
int *ptr;
ptr = &x;

This means that address of x is stored in ptr.
address
Therefore, ptr is pointing to integer. The advantage of this is you could change value in x via pointer

Ex:

*ptr=50;
printf(“%d”, x); // 50

Similarly, in Javascript the primitive data is saved-as-value. On the contrary, save-as-reference mechanism is there for objects,

Ex:

> var person = {name: "Sumit"};
> var anotherPerson = person;
> person.name = "Ravi";
> console.log(anotherPerson.name); // Ravi
> console.log(person.name); // Ravi

The variable is a reference, not a value

Object Own and Inherited Properties

The own properties are properties that were defined on the object, while the inherited properties were inherited from the object’s Prototype object.

We learnt about own properties in part 1, ex- person.name // own property

Prototypal inheritance – Inherited property

In most OOPS based languages, classes inherit from classes. In Javascript, it’s prototype-based. Objects inherit from Objects. And, we know an object is a prototype of classes. Therefore, it’s prototypical based inheritance

Q: How do we implement prototypal inheritance?

Ans: Use property called __proto__

Ex:

> var automobile = {engine: true};
> var car = {tyres: 4};

Now, we know car is a type of automobile, so we could inherit from automobile.

> car.__proto__ = automobile; //inheritance
> car.engine; // true

Hence, there is CAR => Automobile (car is a automobile)

automobile

Enumerable

Non-Enumerable

Own properties are enumerable. This means you can iterate over these

Inherited properties are non-emunerable. These are hidden unless asked specifically

Ex- car.tyres

Ex car.engine

Object.keys (car) //tyres;

 

car.propertyIsEnumerable("tyres"); //true

car.hasOwnProperty("engine"); //false

car.hasOwnProperty("tyres"); // true

car.propertyIsEnumerable("engine"); //false

Q: Is __proto__ only way to prototypal inheritance?

Ans: No, you could you Object to create it,

ex-

var automobile = {engine: true};
var car = Object.create(automobile);

inheritance

Now, you could see car.engine is available

Q: Is there any other way to check property in object?

Ans: Yes, you could use below pattern to validate,

ex-

> "engine" in car; //true
> "valueOf" in {}; //true

 
Read more articles on JavaScript;

Up Next
    Ebook Download
    View all
    Learn
    View all