"这"关键字继承解决方法 [英] "this" Keyword Inheritence Workaround

查看:74
本文介绍了"这"关键字继承解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,另一个问题是专家。 :D


我有这个的问题这种情况下的关键字。我之前使用窗口属性的方式是使用窗口属性,但在这种情况下,我试图将所有内容保留在对象的上下文中。


所以......基本上,这是指我之前想要它引用的内容(好吧,它在Mozilla中有效,但在IE中没有,所以我使用窗口解决方法来适应两种浏览器)。


我想做的显然是模仿IE(gasp)在其他浏览器中使用attachEvent做的事情。在我的几个函数中引用它们所属的对象,而不是它们被调用的元素。


问题行(我认为):95,96 ,120,219(需要引用整个函数),449,466(需要引用整个函数),483,491,549,603(需要引用整个函数),623,676(整个函数需要参考),761,770(需要引用整个函数),791(需要引用整个函数)。


这些是特定的行(在order):

Alright, another question to pin on the experts. :D

I have an issue with "this" keyword in this case. The way I got around it before was to use a window property, but in this case I''m trying to keep everything within the context of the object.

So... Basically, this is referring to what I wanted it to refer to before (well, it worked in Mozilla, but not in IE, so I used the window workaround to fit both browsers).

What I want to do apparently is to mimic what IE (gasp) does with attachEvent in other browsers to make this. in a couple of my functions to refer to the object they''re a part of, and not the element they were called by.

Problem lines (I think): 95, 96, 120, 219 (the entire function needs to be referenced), 449, 466 (the entire function needs to be referenced), 483, 491, 549, 603 (the entire function needs to be referenced), 623, 676 (the entire function needs to be referenced), 761, 770 (the entire function needs to be referenced), 791 (the entire function needs to be referenced).

Here are those specific lines (in order):

展开 | 选择 | Wrap | 行号

推荐答案

如前所述,事件处理程序和窗口方法中的this关键字不会参考方法的对象(这里有一篇很好的文章: Digital Web杂志 - JavaScript中的范围)但是调用对象(元素或窗口)。您可以使用对象名称(例如chat.method)引用您的方法。你甚至可能想要阅读关于对象的文字。


虽然这篇文章是用德语写的,但它的文献参考值得关注:组织von JavaScripten


问候
as you already mentioned, the this keyword in event handlers and window methods doesn''t refer to the method''s object (quite a good article here: Digital Web Magazine - Scope in JavaScript) but the calling object (element or window). you can reference your methods there with the objects name (e.g. chat.method). you might even want to read about the object literal.

although the article is written in german, its literature reference is worth looking at: Organisation von JavaScripten

regards


只需添加var = this,并在您的事件函数中替换单词this。用那个


使用单独的单词可以减少混淆。
simply add var that=this, and in your event functions replace the word "this" with "that".

there is less confusion using separate words like that.


var that = this; fix是常见的并且可能是修复执行上下文的最佳方法......另一种更复杂的方式可能是 apply() call()方法......这些问题值得一看...


亲切的问候
the var that = this; fix is the common and probably best way to fix the execution context ... another and a bit more complicated way could be the apply() or call() method ... that are worth a look at for such problems ...

kind regards

这篇关于"这"关键字继承解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆