简体   繁体   English

JavaScript 中“this”的使用让我感到困惑

[英]The use of “this” is confusing me in JavaScript

Working with the JavaScript one of the confusing thing is when using this使用 JavaScript 的一个令人困惑的事情是使用this

var x = {  
  ele : 'test',
  init : function(){ 
    alert(this.ele);
  }
}

However when dealing with multiple object and especially events context of this changes and becomes confusing to keep track/understand.然而,随着多个对象,特别是事件的情况下打交道时, this变化并变得扑朔迷离跟踪/理解。

So if anybody has better inputs/guidelines/thoughts/better practices, please share.因此,如果有人有更好的输入/指南/想法/更好的做法,请分享。 Also I would like know if using this gives any (performance) advantage or what?另外我想知道使用this有任何(性能)​​优势或什么?

It's not about performance, it's about accessing a property of a specific instance of an object:-这与性能无关,而是关于访问对象特定实例的属性:-

x.init()

Would not display 'test' if you hadn't use this in the function.如果您没有在函数中使用this ,则不会显示“测试”。

Effectively the above line is the same as:-实际上,上述行与以下内容相同:-

x.init.call(x);

the first paramater in the use of call is assigned to this when the function is executed.当函数执行时, call使用中的第一个参数被分配给this

Now consider:-现在考虑:-

var fn = x.init;  //Note no () so the function itself is assigned to the variable fn
fn();

Now you get nothing in the alert.现在你什么都没有收到警报。 This because the above is effectively:-这是因为以上有效:-

fn.call(window);

In browser hosted Javascript the window object is synonymous with the global object.在浏览器托管的 Javascript 中, window对象与全局对象是同义词。 When a function is called "in the raw" then the this defaults to the global object.当一个函数在“原始”中被调用时, this默认为全局对象。

The classic error is doing something like this:-经典的错误是做这样的事情:-

var x = {
   ele: 'test';
   init: function(elem) { 
      elem.onclick = function() { alert(this.ele); }
   }
}
x.init(document.getElementById('myButton'));

However this doesn't work because the function attached to the onclick event is called by the browser using code like:-但是这不起作用,因为附加到 onclick 事件的函数是由浏览器使用如下代码调用的:-

onclick.call(theDOMElement)

Hence when the function is running this isn't what you think it is.因此,当函数运行时, this不是您认为的那样。

My usual solution to this situation is:-我对这种情况的通常解决方案是:-

var x = {
   ele: 'test';
   init: function(elem) {
      var self = this; 
      elem.onclick = function() { alert(self.ele); }
      elem = null;
   }
}
x.init(document.getElementById('myButton'));

Note the elem = null is IE memory leak work-around.注意elem = null是 IE 内存泄漏的解决方法。

It is very confusing.这很令人困惑。 It depends on how you call the function.这取决于您如何调用该函数。 Doug Crockford did a good write-up in his book Javascript, the Good Parts . Doug Crockford 在他的书Javascript 中写了一篇很好的文章,好的部分 The gist of it is in this excellent answer to an otherwise badly formulated question.它的要点在于对一个其他表述不当的问题的出色回答

And no, it's not about performance.不,这与性能无关。

To me, it helped a lot the following guideline: every time you see this think owner .对我来说,它对以下指导方针有很大帮助:每次你看到this think owner The object who owns the variable name to which the function is assigned will become the this .拥有函数所分配的变量名的对象将成为this If you cannot make sense to who owns it, then this will be window.如果您无法弄清楚谁拥有它,那么this将是窗口。

A good and enlightening article on the this keyword is this (no pun intended).关于this关键字的一篇很好的、有启发性的文章是这样的(没有双关语)。 The article may clear things up for you, I know it did for me.这篇文章可能会为您解决问题,我知道它对我有用。

The essential rule is that the this keyword inside a function always refers to the function owner, and the key to understanding the consequences is understanding when functions are referred and when they are copied .基本规则是函数内的this关键字总是指函数所有者,理解结果的关键是理解函数何时被引用和何时被复制 See the beforementioned article for examples.有关示例,请参阅前面提到的文章。

use

var me = this;

outside of the在外面

function(){

then you can refer to me inside the function()那你可以在function()里面找我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM