重写JS中函数内部的变量
Overwriting variables inside functions in JS
如果这是一个愚蠢的问题,我很抱歉,我还是JS的新手,为什么覆盖变量在函数中不起作用?
var o = {x: 1};
function foo(){
// Overwrite var `o` with a string
arguments[0] = "string";
}
foo(o);
console.log(o); // Why is `o` still an object and not the string `string`?
--基于响应的更新---
但为什么这会奏效呢?
// Define an array literal
var a = ["a", "b"];
// Execute whatever function is passed to manipulate the array
a.execFunc = function(func){
func(this) // Pass this array (object) to it
}
// Invoke `execFunc` function passing another function
a.execFunc(function(thisArray){
thisArray[0] = "c"; // Change first element to "c"
})
console.log(a); // => ["c", "b", execFunc: function]: IT WORKED! a → c
---更新#2----
很抱歉,我只是想把注意力集中在所说的话上——为什么这不起作用?这不是基本上和上面一样吗?我正在像上面一样修改原始数组,而不是arguments
对象,不是吗?
// Define an array literal
var a = ["a", "b"];
// Execute whatever function is passed to manipulate the array
a.execFunc = function(func){
func(this[0]) // Pass the first element (object property) to it
}
// Invoke `execFunc` function passing another function
a.execFunc(function(firstElement){
console.log(firstElement = "c"); // Change first element to "c"
})
console.log(a); // => ["a", "b", execFunc: function]: first element is still `a`
将对象作为参数传递给函数时,arguments
特殊对象将包含对实际对象的引用。当你做
arguments[0] = ...
您只是让0处的元素引用不同的对象,而不是更改o
。如果你尝试这个,你可以更好地理解这一点
function foo() {
console.log(arguments);
arguments[0] = "string";
console.log(arguments);
}
输出将是
{ '0': { x: 1 } }
{ '0': 'string' }
正如您所看到的,0
中的元素先前引用的是o
,而现在已更改为string
。就是这样。
相反,试试这个
function foo() {
arguments[0].x = "string";
}
你会得到
{ x: 'string' }
打印o
时,因为arguments[0]
表示o
,而.x
表示o
中的属性。所以你实际上是在改变o
。这被称为改变对象。
同样,thisArray
引用实际的数组对象,当您将元素更改为0时,实际上是在更改作为参数传递的实际数组。这就是为什么thisArray
中的突变也反映在实际的数组对象中。
如果在函数中更改对象的"属性",javascript将找到引用并对其进行更改。
如果您只是更改对象的'value',javascript会将其视为本地变量,而不是获取引用并对其进行修改。
在第一个例子中,您没有更改o的属性,所以它只是覆盖参数[0],并且没有将任何内容设置为变量o。您需要在函数中返回新值并将其设置为o,应该是这样的:
var o = {x: 1};
function foo(){
// Overwrite var `o` with a string
arguments[0] = "string";
return arguments[0];
}
o = foo(o);
console.log(o);
//will be "string"
或
var o = {x: 1};
function foo(){
// Overwrite var `o` with a string
o = "string";
}
foo();
console.log(o);
//will be "string"
你的第二个是这样的:
var a = ["a","b"];
function foo(){
// Overwrite var `o` with a string
arguments[0][0] = "c";
}
foo(a)
console.log(a)
//will be ["c", "b"]
你倾向于改变数组a的属性,所以它指的是一个数组。但在第一个示例中,您只需更改其值,使其不会引用o对象。
您的更新#2
您只需更改[0]的"值"。所以它不会引用数组a。类似于此:
var a = ["a", "b"];
function foo(){
arguments[0] = "c";
}
foo(a[0]);
console.log(a);
//will be ["a", "b"]
- 如何从函数内部的这个变量中获取值
- 匿名自执行js函数内部的全局变量在外部仍然可用
- Chrome扩展:是否有一种方法可以运行JavaScript来获取页面上的内部html,并将其保存到扩展中的变量中
- 在Javascript中的变量内部使用while()语句
- 变量内部的引号
- 循环和局部变量内部的闭包
- 展开变量内部的标记
- 从构造函数变量内部调用函数,在 JavaScript 中给出 Undefiend 错误
- 为什么没有识别全局变量(内部函数)
- Javascript变量内部数组
- Angularjs $scope变量内部的jquery绑定方法
- 转换& # 39;和“;要在变量内部使用的字符串内部.(Javascript)
- 不能复制变量内部的内容
- 调用变量内部的函数
- 从变量内部的函数访问javascript变量
- 使用Javascript为变量内部的值添加单字符串引号
- JQuery变量内部按钮函数
- 访问变量内部的DOM元素
- 我如何调用$scope变量内部函数在Ionic不同的模板
- JS如何获得一个目标变量内部的变量名