javaScript通过参考与按值
我正在寻找一些良好的综合阅读材料,介绍何时JavaScript通过值以及何时通过引用以及修改传递的项目会影响函数外部的值以及何时不影响。在分配给另一个变量时,我也有兴趣按参考值与按值分配,以及是否遵循与作为函数参数传递的任何不同规则。
我已经进行了很多搜索,并找到了许多具体示例(其中许多示例),我可以从中开始拼凑出真实规则的部分,但是我还没有找到一份单一的,写得很好的文档,描述了这一切。
另外,语言中是否有方法可以控制某物是通过引用还是通过价值传递?
这是我想了解的一些问题。这些只是示例 - 我实际上是在想了解语言所经历的规则,而不仅仅是对特定示例的答案。但是,以下是一些示例:
function f(a,b,c) {
a = 3;
b.push("foo");
c.first = false;
}
var x = 4;
var y = ["eeny", "miny", "mo"];
var z = {first: true};
f(x,y,z);
X,Y和Z的内容何时在所有不同类型的F范围之外更改了F的范围?
function f() {
var a = ["1", "2", "3"];
var b = a[1];
a[1] = "4";
// what is the value of b now for all possible data types that the array in "a" might hold?
}
function f() {
var a = [{yellow: "blue"}, {red: "cyan"}, {green: "magenta"}];
var b = a[1];
a[1].red = "tan";
// what is the value of b now and why?
b.red = "black";
// did the value of a[1].red change when I assigned to b.red?
}
如果我想制作对象的完全独立的副本(没有引用),那么做到这一点的最佳实践方法是什么?
I'm looking for some good comprehensive reading material on when JavaScript passes something by value and when by reference and when modifying a passed item affects the value outside a function and when not. I'm also interested in when assigning to another variable is by reference vs. by value and whether that follows any different rules than passing as a function parameter.
I've done a lot of searching and find lots of specific examples (many of them here on SO) from which I can start to piece together pieces of the real rules, but I haven't yet found a single, well written document that describes it all.
Also, are there ways in the language to control whether something is passed by reference or by value?
Here are some of the types of questions I want to understand. These are just examples - I'm actually looking to understand the rules the language goes by, not just the answers to specific examples. But, here are some examples:
function f(a,b,c) {
a = 3;
b.push("foo");
c.first = false;
}
var x = 4;
var y = ["eeny", "miny", "mo"];
var z = {first: true};
f(x,y,z);
When are the contents of x, y and z changed outside the scope of f for all the different types?
function f() {
var a = ["1", "2", "3"];
var b = a[1];
a[1] = "4";
// what is the value of b now for all possible data types that the array in "a" might hold?
}
function f() {
var a = [{yellow: "blue"}, {red: "cyan"}, {green: "magenta"}];
var b = a[1];
a[1].red = "tan";
// what is the value of b now and why?
b.red = "black";
// did the value of a[1].red change when I assigned to b.red?
}
If I want to make a fully independent copy of an object (no references whatsoever), what's the best practice way to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我的理解是,这实际上很简单:
因此,要浏览一些示例:
示例2:
My understanding is that this is actually very simple:
So, to work through some of your examples:
Example 2:
JavaScript 总是按值通过。但是,如果将对象传递给函数,则“值”实际上是对该对象的引用,因此该函数可以修改该对象的属性,但不会导致函数外部的变量指向某些其他对象。
一个例子:
Javascript always passes by value. However, if you pass an object to a function, the "value" is really a reference to that object, so the function can modify that object's properties but not cause the variable outside the function to point to some other object.
An example:
是的,JavaScript始终按值传递,但是在数组或对象中,值是对其的引用,因此您可以“更改”内容。
但是,我认为您已经读过了。 在这里您拥有所需的文档:
http://snook.ca/archives/javascript/javascript/javascript_pass
Yes, Javascript always passes by value, but in an array or object, the value is a reference to it, so you can 'change' the contents.
But, I think you already read it on SO; here you have the documentation you want:
http://snook.ca/archives/javascript/javascript_pass
按价值。
数组和对象通过引用或基于这两个条件通过值传递或通过值传递。
如果您要使用新对象或数组更改该对象的值或数组,则按值传递。
object1 = {item:“ car”};
array1 = [1,2,3];
在这里,您将新对象或数组分配给旧对象。您没有更改属性的价值
旧对象。因此,它是按值传递的。
如果要更改对象或数组的属性值,则通过参考将其传递。
object1.item =“ car”;
array1 [0] = 9;
在这里您要更改旧对象的属性值。您不是将新对象或数组分配给旧对象
代码
by value.
Array and Object is passed as pass by reference or pass by value based on these two condition.
if you are changing value of that Object or array with new Object or Array then it is pass by Value.
object1 = {item: "car"};
array1=[1,2,3];
here you are assigning new object or array to old one.you are not changing the value of property
of old object.so it is pass by value.
if you are changing a property value of an object or array then it is pass by Reference.
object1.item= "car";
array1[0]=9;
here you are changing a property value of old object.you are not assigning new object or array to old one.so it is pass by reference.
Code