why output is coming on one case and not on the other? both of them have a variable inside their proto , yet I am getting undefined in one case.
Asked
Active
Viewed 481 times
-2
-
1Please post code as text, not as picture. – deceze Jan 30 '19 at 13:01
-
1Possible duplicate of [Understanding the difference between Object.create() and new SomeFunction()](https://stackoverflow.com/questions/4166616/understanding-the-difference-between-object-create-and-new-somefunction) – adiga Jan 30 '19 at 13:09
2 Answers
0
When you type p.constructor.prototype.a
the JavaScript checks for property constructor
in the object itself but it does not have it. When that occurs, it goes over the prototype chain via __proto__
. The problem here is that the object p
and it's prototype(s) does not have property constructor
. This is caused by use of Object.create()
. Difference between it and new
is already described in Understanding the difference between Object.create() and new SomeFunction() as @adiga commented under you post :)

Damian Dziaduch
- 2,107
- 1
- 15
- 16
0
It is very simple.
new a is Object.create(a.prototype)
while Object.create(a) is different than Object.create(a.prototype). new runs constructor code while object does not run constructor.
See following example:
function a(){
this.b = 'xyz';
};
a.prototype.c = 'test';
var x = new a();
var y = Object.create(a);
//Using New Keyword
console.log(x); //Output is object
console.log(x.b); //Output is xyz.
console.log(x.c); //Output is test.
//Using Object.create()
console.log(y); //Output is function
console.log(y.b); //Output is undefined
console.log(y.c); //Output is undefined

Muzaffar Mahmood
- 1,688
- 2
- 17
- 20