2

Objects created using Object.create(someObj.prototype) has it's constructor as someObj, then how come when I try to access the properties of someObj, then it gives as undefined?

function foo(){
    this.name1 = "Name";
    this.otherName1 = "someOtherName";
}

var fooObj = new foo();
console.log(fooObj.name1); // Name

var barObj = Object.create(foo.prototype);

console.log(barObj.constructor);  
   //Outouts: 
  // function foo(){
 //    this.name1 = "Name";                                                                                                         

 //      this.otherName1 = "someOtherName" ;
//    }


//Then why not able to access this?
console.log(barObj.name1); Outputs; // undefined
nitte93
  • 1,820
  • 3
  • 26
  • 42
  • `this.name1` is a property attached directly to the object, not it's prototype, that's why it's undefined, as it should be – adeneo Dec 03 '15 at 20:40
  • I get your point, Do mean the object created using Object.create() won't even be able to access it's constructors property? – nitte93 Dec 03 '15 at 20:43
  • Sure it does, if you prototype the properties, not just attach them to the object. That's why we have prototyping (at least one of the reasons), to inherit. – adeneo Dec 03 '15 at 20:45
  • http://jsfiddle.net/pokjkpzb/ – adeneo Dec 03 '15 at 20:45

2 Answers2

3

It's simply because you haven't called the constructor yet.

Consider the following:

barObj.constructor(); // <--- this will call the constructor function and set this.name1 and this.otherName1
console.log(barObj.name1); // Outputs: "Name"
Nick Zuber
  • 5,467
  • 3
  • 24
  • 48
  • AHHH, That makes sense. – nitte93 Dec 03 '15 at 20:49
  • 1
    To summarize: Object.create() doesn't call the constructor, thus constructor properties were not initialized. Hence Undefined property, right? In that case let's assume, If I create *var y = Object.create(foo);* will that call the constructor, Or how'll it work? OR is it that Object.create() should always be of the form *Object.create(foo.prototype)*. Thanks. – nitte93 Dec 03 '15 at 20:57
  • The `Object.create();` will create a duplicate of that object, which means it will inherit that object's prototypes. Now you have a variable that holds this new object that you've just created, so if you want to initialize it, you'd need to call the constructor. This is similar to how you initialize `foo` when you call `new`. – Nick Zuber Dec 03 '15 at 21:11
  • 1
    @nitte93user3232918: No, `Object.create` does not call anything, it just creates a new, empty object, which inherits from the one passed as an argument. In your case, `barObj` inherits the `.constructor` property from the `foo.prototype` object. – Bergi Dec 03 '15 at 21:17
  • @Bergi My explanation must not have been very clear - thank you for clarifying. I meant that `create()` created a duplicate of `foo` because he *passed* it in as a parameter. – Nick Zuber Dec 03 '15 at 21:20
  • @NickZ: I would personally avoid the term "duplicate" here, it's too similar to "clone" which denotes copying the properties, as opposed to setting up inheritance. – Bergi Dec 03 '15 at 21:25
0

Here barObj is just an object linked to foo prototype object.Object.create won't call foo function until you call it explicitly. See below code.

foo.call(barObj) will call foo function with barObj as it's context. Means this in foo function refers to barObj.

function foo(){
    this.name1 = "Name";
    this.otherName1 = "someOtherName";
}

var fooObj = new foo();
//console.log(fooObj); // Name

var barObj = Object.create(foo.prototype);
foo.call(barObj)

//Outouts: 
// function foo(){
//  this.name1 = "Name";                                                                                                         

//  this.otherName1 = "someOtherName" ;
//}


//Then why not able to access this?
console.log(barObj.name1);
SeniorJD
  • 6,946
  • 4
  • 36
  • 53
Venkat
  • 551
  • 6
  • 17