javascript - 使用 var that = this 时是复制还是引用?

标签 javascript

当我们使用var that = this时,是复制还是引用。看起来 this 是一个特殊的对象,因此 that 应该是一个引用。这意味着当this改变时,that也应该改变?显然情况并非如此,因为 that 保持不变。

有人可以澄清一下吗?为什么 var that = this 是一种“值副本”?

最佳答案

它将一个称为对象引用的值从 this 复制到变量 that。这并不是说它是一个特殊的对象,这就是 JavaScript 中所有对象的引用方式(包括数组和函数):变量中存储的内容(参数、属性等)是引用到内存中其他地方的对象,而不是对象的副本。

假设你有这个:

var a = {
    answer: 42
}; 

这会创建一个新对象并将引用存储在a中。在内存中你有类似这样的东西:

                +−−−−−−−−−−−−+
a:Ref3554−−−−−−>|  (object)  |
                +−−−−−−−−−−−−+
                | answer: 42 |
                +−−−−−−−−−−−−+

There I've used Ref3554 to represent the reference value, but that's purely conceptual; you never see the actual value in code.

When you do this:

var b = a;  // Copies the value 

它将a(引用)中的值复制到b:

a:Ref3554−−−+
            |   +−−−−−−−−−−−−+
            +−−>|  (object)  |
            |   +−−−−−−−−−−−−+
b:Ref3554−−−+   | answer: 42 |
                +−−−−−−−−−−−−+

It's the same in your example, just with this instead of a and that instead of b.

That means when this changes, that should also change?

this can't change within a scope. But a could, so let's continue with a. Suppose I do this:

a = {
    question: "Life, the Universe, and Everything!"
};

我创建了一个新对象并将其分配给ab 会发生什么?

什么都没有:

                +−−−−−−−−−−−−−−−+
a:Ref4269−−−−−−+|   (object)    |
                +−−−−−−−−−−−−−−−+
                | question: ... |
                +−−−−−−−−−−−−−−−+

                +−−−−−−−−−−−−+
b:Ref3554−−−−−−>|  (object)  |
                +−−−−−−−−−−−−+
                | answer: 42 |
                +−−−−−−−−−−−−+

Notice how a got a new reference (Ref4269). This has no effect whatsoever on b. a and b aren't linked in any way (nor are this and that in your example). They just happened to contain the same value for a while, and then (as of the above), they didn't anymore, because one of them got changed.

But let's go back to when they were both pointing at the same thing:

var a = {
    answer: 42
};
var b = a;
a:Ref3554−−−+
            |   +−−−−−−−−−−−−+
            +−−>|  (object)  |
            |   +−−−−−−−−−−−−+
b:Ref3554−−−+   | answer: 42 |
                +−−−−−−−−−−−−+

Suppose I change the state of the object that both a and b refer to?

a.answer = 27;

b 会发生什么?嗯,b 没有改变,但是它指向的对象与 a 指向的对象是同一个对象,并且该对象的状态刚刚改变,所以你自然会看到新的状态无论您使用哪个变量来访问该对象:

a:Ref3554−−−+
            |   +−−−−−−−−−−−−+
            +−−>|  (object)  |
            |   +−−−−−−−−−−−−+
b:Ref3554−−−+   | answer: 27 |
                +−−−−−−−−−−−−+

关于javascript - 使用 var that = this 时是复制还是引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53672623/

相关文章:

javascript - 将数组从javascript发送到Spring Controller

javascript - 两个 Backbone.Router 定义之间的差异

javascript - Ember-Data 中的多个多对多关系

javascript - 为什么我的 z-index 不起作用?

javascript - 用 Javascript 计算假期

javascript - 如何使用双递归在 AngularJS 中创建菜单? (功能+模板)

javascript - HTML5 从中心点旋转图像

javascript 未在 Rails 编辑页面上执行

javascript - 将回调函数连接到 body 元素失败的 onload 事件

javascript - 没有 "z"的 momentjs toISOString