javascript - What is the difference between object assignment '=' and Object.create() - Stack Overflow

I am going some deep into javascript object operations.Here my question is what is a different between

I am going some deep into javascript object operations. Here my question is what is a different between const me = Object.create(person); and const me = person; here both operation gives me a slimier output. I mean it references object to new variable me.

const person = {
  isHuman: false,
  printIntroduction: function() {
    console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`);
  }
};

const me = Object.create(person);

me.name = 'Matthew';  
me.isHuman = true;

me.printIntroduction();

const me2 = person;

me.name = 'Manan'; 
me.isHuman = false; 

me.printIntroduction();

I am going some deep into javascript object operations. Here my question is what is a different between const me = Object.create(person); and const me = person; here both operation gives me a slimier output. I mean it references object to new variable me.

const person = {
  isHuman: false,
  printIntroduction: function() {
    console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`);
  }
};

const me = Object.create(person);

me.name = 'Matthew';  
me.isHuman = true;

me.printIntroduction();

const me2 = person;

me.name = 'Manan'; 
me.isHuman = false; 

me.printIntroduction();

In above code I have included both operation direct assignment and assign by using Object.create();. Here both variable referencing to objects person, but what is different between it? Can some one explain me? This question might be asked before but I cant find proper explanation. Simple explanation would be appreciated :-).

Share Improve this question edited Jun 14, 2020 at 22:47 Majid Savalanpour 1,8232 gold badges17 silver badges28 bronze badges asked Jun 14, 2020 at 20:51 manan5439manan5439 9589 silver badges27 bronze badges 6
  • 4 If you print out person. You will know the diference – user3562932 Commented Jun 14, 2020 at 20:53
  • @user3562932 could you tell me how it would be different in brief? – code7004 Commented Jun 14, 2020 at 21:00
  • i thing Object.create(person) i creating new object instead of assigning reference of object ? is it? – code7004 Commented Jun 14, 2020 at 21:01
  • See the documentation. In particular, the supplied object is used as the prototype chain: “The Object.create() method creates a new object, using an existing object as the prototype [chain] of the newly created object.” – user2864740 Commented Jun 14, 2020 at 21:03
  • In both cases an object (new or otherwise) is assigned. – user2864740 Commented Jun 14, 2020 at 21:07
 |  Show 1 more ment

3 Answers 3

Reset to default 5

The first difference is that, when you use regular assignment both of your variables point to the same object, when you edit one, you edit the other. This does not happen with create.

const a = {};
const b = a;

b.do = 100;

console.log(a);

const c = Object.create(a);
c.dodo = 100;
console.log(a)

The 2nd difference, is that Object.create creates an object which has the first oject as a "prototype". Prototypes are the basis of how objects and inheritance works in javascript, for example when you have an object, it's default toString method is in the prototype. See this below

const a = {do : 100};
const c = Object.create(a);
console.log(c.do);
console.log(a.hasOwnProperty("do"));
console.log(c.hasOwnProperty("do"));

If yout run the above in a browser console and then log c, you will see that the do is in the __proto__ of c. not directly on c.

Whenever you have any object in javascript and call a property or a method on it, javascript will search it on that object an then go up the prototype chain. This allows you to save space so not every object has to carry the shared properties on it.

Fun side note, {} has all the functions objects have in its prototype, null does not so

const a = Object.create(null);

// works as normal
console.log(a);
a.hello = "hello";
console.log(a);

// error
console.log(a.toString());
console.log(m + m);

EDIT : Sorry, slight mistake when you use Object.create and the edit the original, you do see the change appear in the prototype of the new.

Remember, person is a reference to an object. When you do Object.create(), you are creating a new object (i.e., a new reference) from an existing object. This new object has the original object as a prototype. You assign this new reference to me, and you can modify it without changing the original person object.

On the other hand, me2 = person, assigns the reference to the person object to me2. This means that me2 and person both refer to the same object, and changing one will change the other. You see this in action when you pass objects to functions.

In short, Object.create() should be used when you want to create a new object, and assignment should be used when you want to reference an existing object from a new variable.

The Object.create() method creates a new object, using an existing object as the prototype of the newly created object Object.create

Mean when you are doing

const me = Object.create(person);
   // you are actually doing  
    me={}
    me.__proto__=person

var person = {
  isHuman: false,
  printIntroduction: function() {
  
    console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`);
    
  }
};

const me = Object.create(person);
console.log('isHuman is own Property  obj me:'+me.hasOwnProperty('isHuman'))
me.name = 'Matthew';  
me.isHuman = true;

me.printIntroduction();
debugger


const me2 = person;
console.log('isHuman is own Property obj me2:'+me2.hasOwnProperty('isHuman'))


me2.name = 'Manan'; 
me2.isHuman = false; 

me2.printIntroduction();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745604583a4635598.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信