this

Why doesn't this closure have access to the 'this' keyword? - jQuery

南笙酒味 提交于 2019-11-27 22:14:29
I'm a beginner to closures (and Javscript in general), and I can't find a satisfactory explanation as to what's going on in this code: function myObject(){ this.myHello = "hello"; this.myMethod = do_stuff; } function do_stuff(){ var myThis = this; $.get('http://example.com', function(){ alert(this.myHello); alert(myThis.myHello); }); } var obj = new myObject; obj.myMethod(); It will alert 'undefined' and then 'hello'. Obviously this should not be jQuery specific, but this is the simplest form of my original code I could come up with. The closure in do_stuff() has access to the variables in

Is there a name for “this” in Java?

这一生的挚爱 提交于 2019-11-27 21:32:58
问题 Eclipse will give an error, "The left-hand side of an assignment must be a variable", when I try something like: public class Thing{ String a1; int a2; public void meth(){ Thing A = new Thing(); this = A; } } I had to assign each variable ( this.a1 = A.a1; this.a2 = A.a2; ) as a work around. Are there other ways to do this without going through each variable field? And if this is not a variable what is it called? 回答1: this is a pseudo-variable that points to the current instance of the object

What is the current element in Javascript? [duplicate]

这一生的挚爱 提交于 2019-11-27 21:20:27
问题 This question already has answers here : How may I reference the script tag that loaded the currently-executing script? (13 answers) Closed 5 years ago . How can I find out what the element is that a <script> sits in? As an example, let's take this <div> <script type="text/javascript"> var time = new Date(), hrs = time.getHours(), min = time.getMinutes(); document.write('It is '+hrs+":"+(min<10?'0':'')+min); </script> </div> Then if I want to change this to something more modern, how can I

Why can we use 'this' as an instance method parameter?

自作多情 提交于 2019-11-27 19:24:00
What is receiver parameter in Java ? Java 8 Language Specification talks about this . The JLS gives a hint : Either way, the receiver parameter exists solely to allow the type of the represented object to be denoted in source code, so that the type may be annotated. These two methods are equivalent: class Test { void m1() { } void m2(Test this) { } } However the latter allows you to add annotations: void m2(@MyAnnotation Test this) { } //where MyAnnotation can be defined like this for example: @Target(ElementType.TYPE_USE) @interface MyAnnotation {} Debasis Receiver parameters allow to pass

What are all the differences between function and constructor function in JavaScript?

半城伤御伤魂 提交于 2019-11-27 19:08:11
In this blog author says below function is a constructor function : function Cat(name, color) { this.name = name; this.color = color; } Cat.prototype.age = 0; var catC = new Cat("Fluffy", "White"); The instances of Cat function has a name and colour property. Is this the only difference between normal and constructor function? A constructor function is a normal function. What makes the difference here is the use of the new operator which makes the context ( this ) in the function the new instance, thus letting it take the two properties, and returns this new instance. Without the new operator,

Usage of Java this keyword

人盡茶涼 提交于 2019-11-27 18:25:33
问题 In a class constructor, I am trying to use: if(theObject != null) this = theObject; I search the database and if the record exists, I use theObject generated by Hibernate Query. Why can't I use this ? 回答1: this is not a variable, but a value. You cannot have this as the lvalue in an expression. 回答2: It's because 'this' is not a variable. It refers to the current reference. If you were allowed to reassign 'this', it would no longer remain 'this', it would become 'that'. You cannot do this. 回答3

Hide all but $(this) via :not in jQuery selector

点点圈 提交于 2019-11-27 17:19:17
Advanced title, simple question: How can I do the following in jQuery (hiding everything except $(this) )? $("table tr").click(function() { $("table tr:not(" + $(this) + ")").hide(); // $(this) is only to illustrate my problem $("table tr").show(); }); $(this).siblings().hide(); Traversing/Siblings $("table.tr").not(this).hide(); As an aside, I think you mean $("table tr") (with a space instead of a dot). The way you have it, it selects every table which has a class of tr (eg, <table class="tr"> ), which is probably not what you want. For more information, see the documentation . If you want

Why does String.prototype log it's object like a standard object, while Array.prototype logs it's object like a standard array?

点点圈 提交于 2019-11-27 16:31:21
Simply why does String.prototype log the string object with the standard curly brackets and key value pairs, and the Array.prototype log the array object just like an array, with square brackets and values? String.prototype.test = function(){ console.log(this); // logs { '0': 't', '1': 'e', '2': 's', '3': 't' } }; var str = 'test'; str.test(); Array.prototype.test1 = function(){ console.log(this); // [1,2,3,4] }; var arr = [1,2,3,4]; arr.test1(); Because in a method call the this argument is always (in sloppy mode) casted to an object. What you see is a String object, which was produced from

In C++, initialize a class member with 'this' pointer during construction

浪尽此生 提交于 2019-11-27 16:28:24
问题 I'd like to create a class that is associated to another class in some sort of parent-child relationship. For this the "child" class needs a reference to it's parent. For example: template <typename T> class TEvent { private: T* Owner; public: TEvent(T* parent) : Owner(parent) {} }; class Foo { private: TEvent<Foo> Froozle; // see below }; Now the problem is that I can't initialize the Froozle instance directly, nor using the instanciation list of Foo's constructor, because this references

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

浪子不回头ぞ 提交于 2019-11-27 16:04:32
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 The grouping operator does not destroy property references, which are provoking the method call. This is explicitly mentioned in the spec : NOTE: This algorithm does not apply GetValue to the result of