这是 JavaScript 模式吗?以下代码更改 test.get_context() 对象:
var test = {},
value = {};
test.get_context = function(val) {
return function() {
return val;
}
}(value);
var context = test.get_context();
context["test"] = 123;
//context = { "test": 123};
console.log(test.get_context());
但以下内容不会改变它:
var test = {},
value = {};
test.get_context = function(val) {
return function() {
return val;
}
}(value);
var context = test.get_context();
//context["test"] = 123;
context = { "test": 123};
console.log(test.get_context());
在这种情况下有什么区别
context["test"] = 123;
和
context = { "test": 123};
最佳答案
context["test"]
将 context.test
设置为 123
。
context = { "test": 123}
将上下文替换为新对象,其中 test
= 123
这是更新值和替换变量之间的区别。
您的第一个版本更新对象,而第二个版本覆盖它。
关于javascript - 这是 JavaScript 模式吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24758361/