在Object文字中调用了什么?

时间:2022-09-06 00:21:28

so my question is when using an object literal inside a method I see people doing 'this.something = ...'

所以我的问题是当在方法中使用对象文字时,我看到人们正在做'this.something = ...'

And then referencing it throughout their object. What's the name for this?

然后在整个对象中引用它。这是什么名字?

For e.g. Here in the example below if you look in the 'CacheDom' method 'this.button = document.getElementById('submit')' gives us a reference we can use later.

对于例如在下面的示例中,如果您查看'CacheDom'方法'this.button = document.getElementById('submit')'为我们提供了一个我们以后可以使用的参考。

I understand the basics of the 'this' keyword and inside an object it will reference to that object, but I found it strange to be able to store elements etc and reference them later.

我理解'this'关键字的基础知识,并在对象内部引用该对象,但我发现能够存储元素等并稍后引用它们很奇怪。

Essentially what is the official term for this?

基本上这是什么官方术语?

Thanks

https://jsfiddle.net/rvs6ymqj/

HTML

<body>
  <button id="submit" type="submit">Submit</button>
</body>

JS

var obj = {
  init: function() {
    this.cacheDom();
    this.bindEvents();
  },

  cacheDom: function() {
    this.button = document.getElementById('submit');
  },

  bindEvents: function() {
    this.button.addEventListener("click", function() {
      console.log("we clicked the button");
    })
  }
}

obj.init();

2 个解决方案

#1


1  

It varies from language to language. In Javascript, it's called a property. In Java, they're fields. C++ also calls them fields, though they may also be called data members.

它因语言而异。在Javascript中,它被称为属性。在Java中,它们是字段。 C ++也称它们为字段,但它们也可称为数据成员。

JS is notably different from the other two in that Java and C++ force you to declare your fields, while JS members are defined on the fly. Really, JS objects are just maps from keys to values: the properties are the keys.

JS与其他两个明显不同,Java和C ++强制您声明字段,而JS成员是动态定义的。实际上,JS对象只是从键到值的映射:属性是键。

#2


1  

In Object-Oriented Programming, "this" refers to the object you are inside. You aren't storing properties in "this". It is a way to differentiate between global variables/constructor parameters and instance variables of the same name. I don't think there's a specific name for the concept of "this" but this phrase sums it up nicely:

在面向对象编程中,“this”指的是你所在的对象。您没有在“this”中存储属性。它是一种区分全局变量/构造函数参数和同名实例变量的方法。我不认为“this”的概念有一个特定的名称,但是这句话总结得很好:

'this' is usually an immutable reference or pointer which refers to the current object

'this'通常是一个不可变的引用或指针,它引用当前对象

https://en.wikipedia.org/wiki/This_(computer_programming)

Javascript-Specific Explanation:

JavaScript

When used outside any function, in global space, this refers to the enclosing object, which in this case is the enclosing browser window, the window object. When used in a function defined in the global space, what the keyword this refers to depends on how the function is called. When such a function is called directly (e.g. f(x)), this will refer back to the global space in which the function is defined, and in which other global functions and variables may exist as well (or in strict mode, it is undefined). If a global function containing this is called as part of the event handler of an element in the document object, however, this will refer to the calling HTML element. When a method is called using the new keyword (e.g. var c = new Thing()) then within Thing this refers to the Thing object itself. When a function is attached as a property of an object and called as a method of that object (e.g. obj.f(x)), this will refer to the object that the function is contained within. It is even possible to manually specify this when calling a function, by using the .call() or .apply() methods of the function object. For example, the method call obj.f(x) could also be written as obj.f.call(obj, x). To work around the different meaning of this in nested functions such as DOM event handlers, it is a common idiom in JavaScript to save the this reference of the calling object in a variable (commonly called that or self), and then use the variable to refer to the calling object in nested functions.

当在任何函数之外使用时,在全局空间中,这指的是封闭对象,在这种情况下是封闭的浏览器窗口,即窗口对象。当在全局空间中定义的函数中使用时,它引用的关键字取决于函数的调用方式。当直接调用这样的函数时(例如f(x)),这将返回定义函数的全局空间,并且其中也可能存在其他全局函数和变量(或者在严格模式下,它是未定义)。但是,如果将包含this的全局函数作为文档对象中元素的事件处理程序的一部分进行调用,则它将引用调用HTML元素。当使用new关键字调用方法时(例如var c = new Thing()),那么在Thing中,这指的是Thing对象本身。当函数作为对象的属性附加并作为该对象的方法(例如obj.f(x))调用时,这将引用该函数所包含的对象。通过使用函数对象的.call()或.apply()方法,甚至可以在调用函数时手动指定它。例如,方法调用obj.f(x)也可以写为obj.f.call(obj,x)。要在嵌套函数(如DOM事件处理程序)中解决此问题的不同含义,在JavaScript中将调用对象的此引用保存在变量(通常称为自身或自身)中是一种常见的习惯用法,然后使用该变量引用嵌套函数中的调用对象。

#1


1  

It varies from language to language. In Javascript, it's called a property. In Java, they're fields. C++ also calls them fields, though they may also be called data members.

它因语言而异。在Javascript中,它被称为属性。在Java中,它们是字段。 C ++也称它们为字段,但它们也可称为数据成员。

JS is notably different from the other two in that Java and C++ force you to declare your fields, while JS members are defined on the fly. Really, JS objects are just maps from keys to values: the properties are the keys.

JS与其他两个明显不同,Java和C ++强制您声明字段,而JS成员是动态定义的。实际上,JS对象只是从键到值的映射:属性是键。

#2


1  

In Object-Oriented Programming, "this" refers to the object you are inside. You aren't storing properties in "this". It is a way to differentiate between global variables/constructor parameters and instance variables of the same name. I don't think there's a specific name for the concept of "this" but this phrase sums it up nicely:

在面向对象编程中,“this”指的是你所在的对象。您没有在“this”中存储属性。它是一种区分全局变量/构造函数参数和同名实例变量的方法。我不认为“this”的概念有一个特定的名称,但是这句话总结得很好:

'this' is usually an immutable reference or pointer which refers to the current object

'this'通常是一个不可变的引用或指针,它引用当前对象

https://en.wikipedia.org/wiki/This_(computer_programming)

Javascript-Specific Explanation:

JavaScript

When used outside any function, in global space, this refers to the enclosing object, which in this case is the enclosing browser window, the window object. When used in a function defined in the global space, what the keyword this refers to depends on how the function is called. When such a function is called directly (e.g. f(x)), this will refer back to the global space in which the function is defined, and in which other global functions and variables may exist as well (or in strict mode, it is undefined). If a global function containing this is called as part of the event handler of an element in the document object, however, this will refer to the calling HTML element. When a method is called using the new keyword (e.g. var c = new Thing()) then within Thing this refers to the Thing object itself. When a function is attached as a property of an object and called as a method of that object (e.g. obj.f(x)), this will refer to the object that the function is contained within. It is even possible to manually specify this when calling a function, by using the .call() or .apply() methods of the function object. For example, the method call obj.f(x) could also be written as obj.f.call(obj, x). To work around the different meaning of this in nested functions such as DOM event handlers, it is a common idiom in JavaScript to save the this reference of the calling object in a variable (commonly called that or self), and then use the variable to refer to the calling object in nested functions.

当在任何函数之外使用时,在全局空间中,这指的是封闭对象,在这种情况下是封闭的浏览器窗口,即窗口对象。当在全局空间中定义的函数中使用时,它引用的关键字取决于函数的调用方式。当直接调用这样的函数时(例如f(x)),这将返回定义函数的全局空间,并且其中也可能存在其他全局函数和变量(或者在严格模式下,它是未定义)。但是,如果将包含this的全局函数作为文档对象中元素的事件处理程序的一部分进行调用,则它将引用调用HTML元素。当使用new关键字调用方法时(例如var c = new Thing()),那么在Thing中,这指的是Thing对象本身。当函数作为对象的属性附加并作为该对象的方法(例如obj.f(x))调用时,这将引用该函数所包含的对象。通过使用函数对象的.call()或.apply()方法,甚至可以在调用函数时手动指定它。例如,方法调用obj.f(x)也可以写为obj.f.call(obj,x)。要在嵌套函数(如DOM事件处理程序)中解决此问题的不同含义,在JavaScript中将调用对象的此引用保存在变量(通常称为自身或自身)中是一种常见的习惯用法,然后使用该变量引用嵌套函数中的调用对象。