浅析 Javascript 对象传递

在Js中基本数据类型(也叫原始数据类型:String, Boolean, Number, null, underfined, Symbol(es6新增))都是传值,而非原始类型(也叫引用类型:Object, Array, Function)都是引用类型,传递的都是对象的引用 :Reference.

Pass by Reference

Object in JavaScript passes by reference from one function to another.

Example: JS Object Passes by Reference

function changeFirstName(per) {

    per.firstName = "Steve";

}

var person = { firstName : "Bill" };

changeFirstName(person)    

person.firstName; // returns Steve

=========================================================================

If, two objects point to the same object then the change made in one object will reflect in another object.(如果两个对象同时指向同一个对象,那么改变其中一个便会映射到另一个)

Example: Object Reference

var person = { firstName : "John" };

var anotherPerson = person;  

anotherPerson.firstName = "Bill";

person.firstName; //returns Bill person引用的指向已经改变,指向了anotherPerson,所以值为Bill

=========================================================================

function replace(ref) {

    ref = {}; // this code does _not_ affect the object passed

}

function update(ref) {

    ref.key = 'newvalue';  // this code _does_ affect the _contents_ of the object

}

var a = { key: 'value' };

replace(a);  // a still has its original value - it's unmodfied

update(a);   // the _contents_ of 'a' are changed

=========================================================================

If I were to pass myObject as a parameter to a function, it behaves as if I simply assigned it to a new variable. Again, the same thing with the exact same behavior but with a function.

如果把一个对象作为参数传递给一个函数,这个行为相当于我重新通过函数给它赋值,同理,同样的事情,同样的行为,但是是通过函数

function myFunc(sameObject) {

// We mutate the object, so the myObject gets the change too... just like before.

sameObject.name = 'Jill';

// But, if we re-assign it, the link is lost

    sameObject = { name: 'Howard' };

}

var myObject = { name: 'Joe'; }

// This behaves the same as if we said sameObject = myObject;

myFunc(myObject);

console.log(myObject.name); // Logs 'Jill'

=========================================================================

Every time you pass a variable to a function, you are "Assigning" to whatever the name of the parameter is, just like if you used the equal (=) sign.

Always remember that the equals sign (=) means assignment. And passing a parameter to a function also means assignment. They are the same and the 2 variables are connected in exactly the same way.

The only time that modifying a variable affects a different variable is when the underlying object is mutated.

There is no point in making a distinction between objects and primitives, because it works the same exact way as if you didn't have a function and just used the equal sign to assign to a new variable.

译:每次我们传递一个变量给一个函数,你就在赋值给一个无论参数名是什么的变量,就像你使用 = 赋值一样,时刻记住‘=’ 意味着赋值,而且传递一个参数给一个函数也意味着赋值,它们是一个道理,两个变量以一个具体的方式被连接起来。

修改一个变量影响另一个变量的唯一时间是基础对象发生转变时。

所以没有意义去区分对象和基本数据类型,它都是以一种特定的方式运行就像你根本不用去用一个函数仅仅用赋值号赋新值即可。

发布了69 篇原创文章 · 获赞 35 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/qyl_0316/article/details/103316323
今日推荐