简体   繁体   English

在JavaScript中将一个对象分配给另一个对象并返回对象并将其分配给另一个对象有什么区别?

[英]what is the difference between assigning one object to another object and returning object and assigning it to another object in javaScript?

so if we create two object 所以如果我们创建两个对象

var dog = {
var name:"dog",
    makeSound: function(){
    alert("wow");
    }

}


var cate = {
var name:"cate",
    makeSound: function(){
    alert("meow");
    }

}

cate = dog;

now both cate and dog point to same object. 现在, catedog指向同一个对象。

and if we do it like this 如果我们这样做

cate = object.create(dog);

what is the difference between the two is cate and dog point to same object now? catedog指向同一对象的区别是什么?

object.create object.create

Creates a new object with the specified prototype object and properties. 用指定的原型对象和属性创建一个新对象。

MDN MDN

Not with the same values ! 不能具有相同的

While cate = dog; cate = dog; just tells cate to point to the same reference as dog . 只是告诉cate指向与dog相同的参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM