A Deeper Look at Objects in JavaScript | kirupa.com

by kirupa | 12 December 2013

In the Introduction to Objects in JavaScript tutorial, I provided a very high level overview of what objects in JavaScript are and how to think about them. In this tutorial, we will make that tutorial seem like the tip of a ginormous iceberg:

This is a companion discussion topic for the original entry at http://www.kirupa.com/html5/a_deeper_look_at_objects_in_javascript.htm

Hi Kirupa,

Thanks for this great article which will help many people understand the basics of object oriented programming in JavaScript.

I see that in your diagrams you mention that the [[Prototype]] properties of the objects instantiated references to the object created from which is not true in my opinion. Because when an instance of an object created by new(Constructor), the constructor’s prototype property is assigned to the [[Prototype]] property of that new object.

Example: salesman.prototype should refer to person.prototype (not person)

In other words there is no direct link between an instance and the object its instantiated (which maybe better to call as ‘constructor object’), they are connected by prototype.

instance -> constructor.prototype -> constructor



There’s usually a direct link via the constructor property, or using the instanceof operator.

For example:

var person = function(){}
var salesman = new person
salesman instanceof person // true
salesman.constructor == person // true
salesman.__proto__ == person.prototype // true

Glancing at Kirupa’s tutorial, it looks to me like you’re misinterpreting the diagram. Of course, there are always areas for improvement for Kirupa’s tutorials, so your point should be taken into consideration. The way I read k-man’s diagram is that [[Prototype]] references an object that resembles each newly created Object.

That diagram and/or supporting information can be made clearer, @erolyil :smile: Thanks for bringing that up - I’ll revise it shortly!

Hi again,

This is the point I am objecting already.

'constructor' in salesman // true
salesman.hasOwnProperty("constructor") // false

When you define an object actually two objects are defined. ‘Person’ and ‘Person.prototype’. When a new instance of an object created with new(Constructor), a single object gets created, for example ‘salesman’. ‘salesman’ has a ‘reference variable’ property called [[Prototype]] pointing to the constructors protoype for example ‘Person.prototype’ who has a contructor reference variable pointing to the constructor function, Person() in that case.