this

Using “this” with class name

我的未来我决定 提交于 2019-11-26 11:49:48
问题 I am doing Android programming and was learning about Intents, when I saw a constructor that, to my C# trained mind, seemed funky. The call was: Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class); Both of the parameters are new to me. How is there a static \".this\" off of a Class Name? Is this a Java thing or an Android thing? I am assuming that it is the same as just saying \"this\", since I am in the context of CurrentActivity , but I don\'t get how the \"this\" can be

JavaScript object functions and `this` when unbound and returned in expression/parens

主宰稳场 提交于 2019-11-26 11:38:01
问题 Lines 1-2 and 4-5 make sense in terms of the this returned. What am I missing about line 3? I thought it would return window similar to lines 4-5. Is there another pattern not in these 5 that could help demonstrate why? foo = { bar : function () { return this } } foo.bar() // ==> foo (foo.bar)() // ==> foo / but why? (foo.bar ? foo.bar : $.noop)() // ==> window (foo.bar || 0)() // ==> window 回答1: The grouping operator does not destroy property references, which are provoking the method call.

$(this) doesn't work in a function

回眸只為那壹抹淺笑 提交于 2019-11-26 11:36:30
问题 The following code loads html content from a file (i used this thread) <script> $.fn.loadWithoutCache = function (){ $.ajax({ url: arguments[0], cache: false, dataType: \"html\", success: function(data) { $(this).html(data); // This is not working //$(\'#result\').html(data); //THIS WORKS!!! alert(data); // This alerts the contents of page.html } }); } $(\'#result\').loadWithoutCache(\'page.html\'); </script> Please let me know what the problem is? I hope it\'s something stupid :) Edit:

Javascript lost context when assigned to other variable

Deadly 提交于 2019-11-26 11:31:40
问题 Why in javascript if you reference objects method to some variable it loses that objects context. Can\'t find any link with explanation what happens under the hood. Except this one which states: ‘this’ refers to the object which ‘owns’ the method which doesn\'t seam to be true. var Class = function() { this.property = 1 } Class.prototype.method = function() { return this.property; } var obj = new Class(); console.log(obj.method() === 1); var refToMethod = obj.method; // why refToMethod \'this

&#39;this&#39; keyword, not clear

瘦欲@ 提交于 2019-11-26 11:25:32
问题 I get confused about \' this \' keyword in the following codes, there are two \'this\': var Foo = function(string){ this.name=string // 1st-this } Foo.prototype.get_name = function(){ return this.name // 2nd-this } var myFoo = new Foo(\'John\') the_name=myFoo.get_name() \' the_name \' is equal to \'John\', the prototype method get the name by return this.name . But can anyone explain to me the 1st- this and 2nd- this , what do they stand for? 回答1: In Javascript, the value of this is dependent

Difference between $(this) and this in jquery

痞子三分冷 提交于 2019-11-26 10:38:58
问题 What is the fundamental difference between using $(this) vs this $(\'.viewComments\').click(function(ev){ //returns the desired value alert(this.getAttribute(\'id\')); //Gives an error sayin function is not defined alert($(this).getAttribute(\'id\')); //returns the desired value alert($(this).attr(\'id\')); }); What I thought was \"$(this)\" will contain all functions that \"this\" has and more..But that doesn\'t seem to be the case. So what exactly is $(this)? and Hw do I know what functions

“this” is undefined inside map function Reactjs

梦想的初衷 提交于 2019-11-26 10:17:58
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_view_article', {slug: articles[item.articleId].slug, id: item.articleId}); } else { url = item.url; } return url; },

What is context in _.each(list, iterator, [context])?

萝らか妹 提交于 2019-11-26 10:08:30
问题 I am new to underscore.js. What is the purpose of [context] in _.each() ? How should it be used? 回答1: The context parameter just sets the value of this in the iterator function. var someOtherArray = ["name","patrick","d","w"]; _.each([1, 2, 3], function(num) { // In here, "this" refers to the same Array as "someOtherArray" alert( this[num] ); // num is the value from the array being iterated // so this[num] gets the item at the "num" index of // someOtherArray. }, someOtherArray); Working

Why is assignment to &#39;this&#39; not allowed in java?

纵饮孤独 提交于 2019-11-26 09:10:03
问题 The error I get from the compiler is \"The left hand side of an assignment must be a variable\". My use case is deep copying, but is not really relevant. In C++, one can assign to *this . The question is not how to circumvent assignment to this . It\'s very simple, but rather what rationale is there behind the decision not to make this a variable. Are the reasons technical or conceptual? My guess so far - the possibility of rebuilding an Object in a random method is error-prone (conceptual),

What is an “incompletely constructed object”?

怎甘沉沦 提交于 2019-11-26 09:09:59
问题 Goetz\'s Java Concurrency in Practice, page 41, mentions how this reference can escape during construction. A \"don\'t do this\" example: public class ThisEscape { public ThisEscape(EventSource source) { source.registerListener( new EventListener() { public void onEvent(Event e) { doSomething(e); } }); } } Here this is \"escaping\" via the fact that doSomething(e) refers to the enclosing ThisEscape instance. The situation can be fixed by using static factory methods (first construct the plain