this

Inheritance and the “this” keyword

。_饼干妹妹 提交于 2020-01-01 02:17:50
问题 Suppose that we have next situation: Parent class A: class A{ public A(){} public doSomething(){ System.out.println(this.getClass()); } } with a child class B: class B extends A{ public B(){} public void doSomething(){ super.doSomething(); System.out.println(this.getClass()); } } and Main class: class Main{ public static void main(String[] args){ A ab=new B(); ab.doSomething(); } } When I execute this code result is B B Why does this , referenced in superclass A, returns B as a class when the

Passing “this” to a function from within a constructor?

那年仲夏 提交于 2019-12-31 19:43:11
问题 Can I pass "this" to a function as a pointer, from within the class constructor, and use it to point at the object's members before the constructor returns? Is it safe to do this, so long as the accessed members are properly initialized before the function call? As an example: #include <iostream> class Stuff { public: static void print_number(void *param) { std::cout << reinterpret_cast<Stuff*>(param)->number; } int number; Stuff(int number_) : number(number_) { print_number(this); } }; void

Using this in event handler in strict javascript?

半腔热情 提交于 2019-12-31 04:45:10
问题 Suppose you have a routine like the following to wire up click event handlers getElements(".board>div").forEach(function(elem){ elem.addEventListener("click", handleClick); }); And then in the handler, you need to work with the sender (i.e. this) function handleClick(){ if(this.innerText.toLowerCase() !== "x"){ ... How do you use this in this scenario without a jshint violation/warning? 回答1: Your use of this is valid. To suppress the this errors in your event handler add /*jshint validthis:

Scoping problem with Javascript callback

巧了我就是萌 提交于 2019-12-31 04:37:18
问题 I am having some trouble getting a callback function to work. Here is my code: SomeObject.prototype.refreshData = function() { var read_obj = new SomeAjaxCall("read_some_data", { }, this.readSuccess, this.readFail); } SomeObject.prototype.readSuccess = function(response) { this.data = response; this.someList = []; for (var i = 0; i < this.data.length; i++) { var systemData = this.data[i]; var system = new SomeSystem(systemData); this.someList.push(system); } this.refreshList(); } Basically

Use of 'this' in closure

时光毁灭记忆、已成空白 提交于 2019-12-31 03:20:30
问题 I'm just curious... how am I supposed to use 'this' within a jQuery function? For example, if I have some code like this... headEl.find("form.blog-search input").focus(function() { $(this).next("span").animate({opacity:1}, 200); }) It works fine, however when linting I get the warning of " Use of ' this ' in closure ". Is this something I should just ignore, or is there something I can do to not only solve the warning, but improve my code? Update: Based on Kevin B's comment below, I changed

JavaScript closure and the this object

走远了吗. 提交于 2019-12-30 19:53:13
问题 I thought I had a reasonable understanding of the this object in JavaScript. When dealing with objects, callbacks, and both events and handlers, I haven't had a problem with it since time immemorial. Now, however, all has changed. I've fallen head over heels in love with JavaScript. Pure JS, that is, not jQuery, prototype.js, dojo... So naturally, I've taken to using closures. In some cases, though, this is catching me off guard here. Take this snippet for one: function anyFunc(par) { /

Accessing the containing class of an inner class in Java

旧城冷巷雨未停 提交于 2019-12-30 16:24:26
问题 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. 回答1: You can use what is

Best approach to avoid javascript's “this” mistakes

别说谁变了你拦得住时间么 提交于 2019-12-30 09:31:11
问题 In some cases, the this keyword may not refer to the object I expect it to. (recent example: in an key event, in my XBL) What's the best approach to avoid this kind of mistake? For now, I'm using always the $.fn from jQuery to store my variables, but I'm not sure if it's the best approach. 回答1: Don't avoid using this . Just use it the right way. Javascript is a prototype based object oriented language. If you create your objects using the object prototype should always know what this refers

How to bind methods when destructuring an object in JavaScript?

て烟熏妆下的殇ゞ 提交于 2019-12-30 09:13:38
问题 How to bind methods when destructuring an object in JavaScript? const person = { getName: function() { console.log(this); } }; var a = person.getName; var b = person.getName.bind(person); var {getName: c} = person; person.getName(); //=> {getName: [Function]} a(); //=> window or global b(); //=> {getName: [Function]} c(); //=> window or global I want c to log in the console its "parent" object {getName: [Function]} . Is there any way to bind all methods when destructuring an object in one

How to bind methods when destructuring an object in JavaScript?

左心房为你撑大大i 提交于 2019-12-30 09:10:03
问题 How to bind methods when destructuring an object in JavaScript? const person = { getName: function() { console.log(this); } }; var a = person.getName; var b = person.getName.bind(person); var {getName: c} = person; person.getName(); //=> {getName: [Function]} a(); //=> window or global b(); //=> {getName: [Function]} c(); //=> window or global I want c to log in the console its "parent" object {getName: [Function]} . Is there any way to bind all methods when destructuring an object in one