1

i have created an object as follows

 var lassie = {
 name: 'Lassie',
 breed: 'Collie',
 speak: function () {
 return 'Woof! Woof!'; }
 };

I create another object by using Object.create method of javascript as

var obj = Object.create(lassie);

now when i console obj it gives me a blank object. But when i console obj.speak() it gives me 'Woof! Woof!' .

can someone please explain why?

4 Answers 4

1

Pass lassie as first parameter to Object.create() to set prototype of obj to lassie; iterate lassie return properties as second parameter

var lassie = {
   name: 'Lassie',
   breed: 'Collie',
   speak: function() {
     return 'Woof! Woof!';
   }
 };

 var obj = Object.create(lassie, (function() {
   var _obj = {};
   for (var prop in lassie) {
     _obj[prop] = {
       value: lassie[prop],
       writable: true,
       enumerable: true,
       configurable: true
     }
   }
   return _obj
 }()));

alert(obj.speak())

Sign up to request clarification or add additional context in comments.

5 Comments

Note, you can also pass null as first parameter to Object.create() and still pass enuerable properties as second parameter jsfiddle.net/u1cvnz1f
@AtulAgrawal Depending on expected result could pass original object, null or object literal {} as first parameter to Object.create()
But for the same work why i need to use Object.create as i can directly achieve the same thing with out that
My question is obj is now the reference to an empty object then how it prints the properties of lassie
@AtulAgrawal The first parameter to Object.create() set the prototype for the created object. When you pass lassie , lassie is set as prototype of obj. See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… "propertiesObject Optional. If specified and not undefined, an object whose enumerable own properties (that is, those properties defined upon itself and not enumerable properties along its prototype chain) specify property descriptors to be added to the newly-created object, with the corresponding property names."
1
var obj = Object.create(lassie);

By doing this you have created a new object which inherits the properties from lassie.

if you do

obj.name; //this prints "Lassie" too.

Comments

1

Basically Object.create will create an object that would have no own properties. That means the passed object's own properties would be assigned as the prototype properties of the newly created object and that would be returned.

A sample structure of the object returned from Object.create(),

var obj = {a:10};
var obj1 = Object.create(obj);
console.log(obj1); // {... __proto__ : {a : 10 ...}}

If you want to copy the own propties(enumerable) of the passed object to another one object, then you have to can Object.assign(obj) as an alternate to passing second parameter as property descriptor properties for Object.create

11 Comments

"If you want to create an object with same structure as the passed one, then you have to use Object.assign(obj)"?
@guest271314 What I meant was, if OP want to copy the own properties of the passed object to a newer one. Then he has to use assign. That too enumerable properties.
Object.create() can be used to pass enumerable properties
@guest271314 But the own properties will be converted as prototype properties if we use create. At the same time, assign will copy the own properties as the own properties of the returning object.
Phrase "you have to use" omits other possible solutions?
|
1
var lassie = {
 name: 'Lassie',
 breed: 'Collie',
 speak: function () {
 return 'Woof! Woof!'; }
 };

var obj = Object.create(lassie);

Now your concern is now when You console obj it gives me a blank object. But when you console obj.speak() it gives you 'Woof! Woof!' .

Actually here obj has now all the properties of lassie ,but these are not the obj own properties.

Here you will see all the properties of obj:

for(var a in obj){
     console.log(a);
     }

Here you will see obj's own property:

for(var b in obj){
    if (obj.hasOwnProperty(b)) {    
         console.log(b);
    }
 }

This is actually because when we use var target = Object.create(obj); then all the properties are added to target as a prototype properties.

2 Comments

but why obj.prototype is empty
You have to use obj.__proto_ to access prototype properties.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.