this

Simple javascript to mimic jQuery behaviour of using this in events handlers

与世无争的帅哥 提交于 2019-12-01 17:24:16
问题 This is not a question about jQuery, but about how jQuery implements such a behaviour. In jQuery you can do this: $('#some_link_id').click(function() { alert(this.tagName); //displays 'A' }) could someone explain in general terms (no need you to write code) how do they obtain to pass the event's caller html elments (a link in this specific example) into the this keyword? I obviously tried to look 1st in jQuery code, but I could not understand one line. Thanks! UPDATE: according to Anurag

Is `this` keyword optional when accessing members in C#?

为君一笑 提交于 2019-12-01 16:33:10
I notice that if you have a private member in a class, you can access it in the class methods by just referring to it's name. You do not need to say this.memberName , just memberName works. So is the this keyword optional in the context of member access? I do see it is useful when you want to clarify the scope - when you have 2 variables with the same name. Is there any other reason to use it when accessing members? Yes, it's optional. The only times you'd have to use it are when you have a local variable that hides a member variable, or you want to refer to an indexed property (aka indexer) .

Why is “this” in an ES6 class not implicit?

社会主义新天地 提交于 2019-12-01 15:53:21
I know that ES6 solved a lot of problems that existed with the this keyword in ES5, such as arrow functions and classes. My question relates to the usage of this in the context of an ES6 class and why it has to be written explicitly. I was originally a Java developer and I come from a world where the following lines of code were perfectly natural. class Person { private String myName; public Person() { myName = "Heisenberg"; } public void sayMyName() { System.out.println("My name is " + myName); } } The compiler will always refer to the value of the field myName , unless it has a local

Is `this` allowed inside a noexcept specification?

橙三吉。 提交于 2019-12-01 15:49:33
I have some code which requires me to use *this , but I want it to be noexcept friendly: struct foo; // Would actually be something with conditional noexcept void do_something(foo&); struct foo { void fn() noexcept(noexcept(::do_something(*this))) { ::do_something(*this); } }; However, gcc rejects this : <source>:7:43: error: invalid use of 'this' at top level noexcept(noexcept(::do_something(*this))) If I just access a member, gcc is fine: void do_something(int); struct bar { int x; void fn() noexcept(noexcept(::do_something(x))) { ::do_something(x); } }; However, if I access the member

Referring to “this” in a parent closure in javascript

北城以北 提交于 2019-12-01 15:48:22
I want to do this in Javascript: function Z( f ) { f(); } function A() { this.b = function() { Z( function () { this.c() } ); } this.c = function() { alert('hello world!'); } } var foo = new A(); foo.b(); It can be accomplished this way: function Z( f ) { f(); } function A() { var self = this; this.b = function() { Z( function () { self.c() } ); } this.c = function() { alert('hello world!'); } } var foo = new A(); foo.b(); Is there a better way? Keeping a reference to the parent (like you have) is a good approach, however for your specific example there's no need for the anonymous wrapper, you

Accessing the containing class of an inner class in Java

六月ゝ 毕业季﹏ 提交于 2019-12-01 15:46:38
This is what I'm doing now. Is there a better way to access the super class? public class SearchWidget { private void addWishlistButton() { final SearchWidget thisWidget = this; button.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { // A better way to access the super class? // something like "this.super" ...? workWithWidget(thisWidget); } } } } I'm programming with Google Web Toolkit, but I think this is really a generic Java question. polygenelubricants You can use what is called the qualified this . JLS 15.8.4. Qualified This Any lexically enclosing instance can

Referring to “this” in a parent closure in javascript

倖福魔咒の 提交于 2019-12-01 15:35:29
问题 I want to do this in Javascript: function Z( f ) { f(); } function A() { this.b = function() { Z( function () { this.c() } ); } this.c = function() { alert('hello world!'); } } var foo = new A(); foo.b(); It can be accomplished this way: function Z( f ) { f(); } function A() { var self = this; this.b = function() { Z( function () { self.c() } ); } this.c = function() { alert('hello world!'); } } var foo = new A(); foo.b(); Is there a better way? 回答1: Keeping a reference to the parent (like

Arrow functions return this as the window object

折月煮酒 提交于 2019-12-01 13:36:27
In my program using the function() syntax is returning the this value of a target element, but using an arrow function returns the window object. How are each of these two functions getting their this ? function editTemplates() { //sits within for loop clocksTemplate.gmt[i].addEventListener('keydown', (e) => { console.log(this); //returns window object }); clocksTemplate.gmt[i].addEventListener('keydown', function(e) { console.log(this); //returns element bound to clocksTemplate.gmt }); According to MDN with arrow functions , this should "retain the original meaning from the enclosing context"

Groovy instance.metaclass vs this.metaclass

*爱你&永不变心* 提交于 2019-12-01 13:28:50
I have a following script: task myTask {} class Person { Person() { Person instance = this println this.metaClass.class.name println this.getMetaClass().class.name println instance.metaClass.class.name println instance.getMetaClass().class.name } } Person person = new Person() And the output is : groovy.lang.MetaClassImpl groovy.lang.MetaClassImpl org.codehaus.groovy.runtime.HandleMetaClass org.codehaus.groovy.runtime.HandleMetaClass Can anyone explain to me what is going on? Thanks in advance. Take a look at this class , class Person { def a, b Person() { a = this b = this println "this $this

How to avoid “this” refering to the DOM element, and refer to the object

萝らか妹 提交于 2019-12-01 13:27:53
I have a problem that I can't work around. The context is: I want to have an inheritance chain, and a method of objects that belong to this inheritance has to be a event handler, and at the same time be able to reach the object properties. I am giving a try to writing JavaScript without the "new" word, and using instead Object.create() with some inheritance hierarchy. So first this approach. So I have a blueprint for the rest of my objects (myProto), and then I create objects with Object.create (so that there is no closure where I can do the trick of assigning this to that or self ). Now, when