this

Set “this” variable easily?

跟風遠走 提交于 2019-11-26 02:29:36
问题 I have a pretty good understanding of Javascript, except that I can\'t figure out a nice way to set the \"this\" variable. Consider: var myFunction = function(){ alert(this.foo_variable); } var someObj = document.body; //using body as example object someObj.foo_variable = \"hi\"; //set foo_variable so it alerts var old_fn = someObj.fn; //store old value someObj.fn = myFunction; //bind to someObj so \"this\" keyword works someObj.fn(); someObj.fn = old_fn; //restore old value Is there a way to

difference and when to use getApplication(), getApplicationContext(), getBaseContext() and someClass.this

这一生的挚爱 提交于 2019-11-26 01:56:14
问题 I\'m new to android and I\'m trying to understand the difference between getApplication() , getApplicationContext( ), getBaseContext() , getContext() and someClass.this and especially when to use the these methods in the following code lines: When I launch a toast what is the difference between these and in which cases to I use them? Toast.makeText(LoginActivity.this, \"LogIn successful\", Toast.LENGTH_SHORT).show(); Toast.makeText(getApplication(), \"LogIn successful\", Toast.LENGTH_SHORT)

Difference between $(this) and event.target?

☆樱花仙子☆ 提交于 2019-11-26 01:48:42
问题 I\'m new to jQuery, and was making tabbed panels, following the tutorial in JavaScript and jQuery : The Missing Manual , there\'s that first line when the author does this : var target = $(this); But i tried to do it that way var target = evt.target; and i got that error : Uncaught TypeError: Object http://localhost/tabbedPanels/#panel1 has no method \'attr\' And when i changed evt.target back to $(this) , it worked like a charm. I want to know what\'s the difference between $(this) and evt

Javascript “this” pointer within nested function

烂漫一生 提交于 2019-11-26 01:46:50
问题 I have a question concerning how the \"this\" pointer is treated in a nested function scenario. Say I insert this following sample code into a web page. I get an error when I call the nested function \"doSomeEffects()\". I checked in Firebug and it indicates that when I am in that nested function, the \"this\" pointer is actually pointing to the global \"window\" object - which I did not expect. I must not be understanding something correctly because I thought since I declared the nested

In a templated derived class, why do I need to qualify base class member names with “this->” inside a member function?

做~自己de王妃 提交于 2019-11-26 01:36:15
问题 While I investigate source code of Qt I saw that trolltech guys explicitly use this keyword to access a field on destructor. inline ~QScopedPointer() { T *oldD = this->d; Cleanup::cleanup(oldD); this->d = 0; } So, what\'s the point of this usage? Are there any benefits? Edit: For those who vote for closing this question, I suspect that this usage is for some class inheritance cases A part of QScopedPointer class definition: template <typename T, typename Cleanup = QScopedPointerDeleter<T> >

When you pass &#39;this&#39; as an argument [duplicate]

泄露秘密 提交于 2019-11-26 01:26:16
问题 This question already has an answer here: How does the “this” keyword work? 22 answers I\'m trying to learn about this , and it\'s confusing me a bit here: var randomFunction = function(callback) { var data = 10; callback(data); }; var obj = { initialData: 20, sumData: function(data) { var sum = this.initialData + data; console.log(sum); }, prepareRandomFunction: function() { randomFunction(this.sumData.bind(this)); } }; obj.prepareRandomFunction(); Is this designed to set itself where it is

“this” is undefined inside map function Reactjs

你说的曾经没有我的故事 提交于 2019-11-26 01:14:18
问题 I\'m working with Reactjs, writing a menu component. \"use strict\"; var React = require(\"react\"); var Menus = React.createClass({ item_url: function (item,categories,articles) { console.log(\'afdasfasfasdfasdf\'); var url=\'XXX\'; if (item.type == 1) { url = item.categoryId == null ? \'javascript:void(0)\' : path(\'buex_portal_browse_category\', {slug: categories[item.categoryId].slug}); } else if (item.type == 2) { url = item.articleId == null ? \'javascript:void(0)\' : path(\'buex_portal

Javascript “this” pointer within nested function

馋奶兔 提交于 2019-11-26 00:50:43
I have a question concerning how the "this" pointer is treated in a nested function scenario. Say I insert this following sample code into a web page. I get an error when I call the nested function "doSomeEffects()". I checked in Firebug and it indicates that when I am in that nested function, the "this" pointer is actually pointing to the global "window" object - which I did not expect. I must not be understanding something correctly because I thought since I declared the nested function within a function of the object, it should have "local" scope in relation to the function (i.e. the "this"

JavaScript - Owner of “this”

穿精又带淫゛_ 提交于 2019-11-26 00:38:43
问题 I followed a tutorial for creating a JavaScript stopwatch and am trying to expand it to work with multiple stopwatches (multiple instances of a class). The problem I have is when I am trying to display the current value while the clock is ticking I need to hard code the class instance becasue using \"this\" doesn\'t work (on the line where I am using console.log). I have cut the code down to the minimum to try to understand this aspect, and have pasted what I have below: function Timer(){ var

Why is JavaScript bind() necessary?

泪湿孤枕 提交于 2019-11-26 00:25:48
问题 The problem in example 1 is \'this\' referring to the global name instead of the myName object. I understand the use of bind() in setting the value of this to a specific object, so it solves the problem in example 1, but why does this problem occur in the first place? Is it just the way Javascript was created? I\'m also wondering why example 3 solves the issue and the difference between example 2 and 3. this.name = \"John\" var myName = { name: \"Tom\", getName: function() { return this.name