0

Just trying to understand why every JavaScript object has a constructor property.

Is there any use of constructor property because object is already created?

var MyFunc = function (name){this.name=name}

var instance = new MyFunc("myclass");

now instance has a constructor property which has reference to MyFunc itself. What is use of having constructor property on instance?

Gaurav Singla
  • 1,405
  • 1
  • 17
  • 17

2 Answers2

1

It is (much) more complicated than that : no object, in, JS has a constructor property by default, but their prototype (if the object has a prototype) have one.

Also, be careful : the constructor references a function, but this prototype property is not read-only, so one can easily change the property, therefore do not rely on it.

laruiss
  • 3,780
  • 1
  • 18
  • 29
0

Does this answer your question ?

var dog1= {"a":1}

function Dog ()
{

}


dog2= new Dog();

console.log(dog1.constructor) //function Object() { [native code]...
console.log(dog2.constructor) //function Dog()...

Sometimes you need to know how object was created. ( not to mention that prototype is a constructor property)

Royi Namir
  • 144,742
  • 138
  • 468
  • 792