this

Is it safe to use the “this” pointer in an initialization list?

*爱你&永不变心* 提交于 2019-11-26 18:11:51
问题 I have two classes with a parent-child relationship (the Parent class "has-a" Child class), and the Child class has a pointer back to the Parent . It would be nice to initialize the parent pointer upon construction of the child, as follows: class Child; class Parent; class Child { public: Child (Parent* parent_ptr_) : parent_ptr(parent_ptr_) {}; private: Parent* parent_ptr; }; class Parent { public: Parent() : child(this) {}; private: Child child; } Now, I know people recommend not using this

Passing “this” in java constructor

元气小坏坏 提交于 2019-11-26 17:49:21
问题 Look into the following code: public class ClassA { private boolean ClassAattr = false; public ClassA() { ClassAHandler handler = new ClassAHandler(this); } } public class ClassAHandler extends GeneralHandler { ClassA ca = null; public ClassAHandler(ClassA classa) { this.ca = classa; } } I need to access ClassAattr on some ClassAHandler methods, among other attributes. Is there a way to do so without passing the origin class in the handler constructor. I don't really like how this solution

Typescript “this” inside a class method

时光毁灭记忆、已成空白 提交于 2019-11-26 17:38:51
问题 I know this is probably painfully basic, but i am having a tough time wrapping my head around it. class Main { constructor() { requestAnimationFrame(this.update); //fine } update(): void { requestAnimationFrame(this.update); //error, because this is window } } It appears to be the case that I need a proxy, so lets say using Jquery class Main { constructor() { this.updateProxy = $.proxy(this.update, this); requestAnimationFrame(this.updateProxy); //fine } updateProxy: () => void update(): void

What is the 'global' object in NodeJS

拜拜、爱过 提交于 2019-11-26 17:38:38
问题 I've just seen a weird behaviour of the this keyword in NodeJS environment. I'm listing them with code. I've run this codes with NodeJS v6.x , with a single JavaScript file. While testing with one line of code as follows, whether with or without the 'use strict' statement, this points to an empty object {} . console.log(this) But, when I'm running the statement within a self executing function like, (function(){ console.log(this); }()); It's printing a really big object. Seems to me the

How to reference the caller object (“this”) using attachEvent

Deadly 提交于 2019-11-26 17:28:46
问题 Using the method .attachEvent() in IE, how can I reference the caller object (the element that triggered the event) with this ? In normal browsers, using .addEventListener , the var this points to the element, while in IE it points to the window object. I need it to work with the following code: var element = //the element, doesn't matter how it is obtained element.addAnEvent = function(name, funct){ if(element.addEventListener) // Works in NORMAL browsers... else if(element.attachEvent){

Why is 'this' undefined inside class method when using promises? [duplicate]

女生的网名这么多〃 提交于 2019-11-26 17:27:25
This question already has an answer here: setTimeout and “this” in JavaScript 5 answers I have a javascript class, and each method returns a Q promise. I want to know why this is undefined in method2 and method3 . Is there a more correct way to write this code? function MyClass(opts){ this.options = opts; return this.method1() .then(this.method2) .then(this.method3); } MyClass.prototype.method1 = function(){ // ...q stuff... console.log(this.options); // logs "opts" object return deferred.promise; }; MyClass.prototype.method2 = function(method1resolve){ // ...q stuff... console.log(this); //

Javascript: Do I need to put this.var for every variable in an object?

久未见 提交于 2019-11-26 17:26:06
问题 In C++, the language I'm most comfortable with, usually one declares an object like this: class foo { public: int bar; int getBar() { return bar; } } Calling getBar() works fine (ignoring the fact that bar might be uninitialized). The variable bar within getBar() is in the scope of class foo , so I don't need to say this->bar unless I really need to make it clear that I'm referring to the class' bar instead of, say, a parameter. Now, I'm trying to get started with OOP in Javascript. So, I

this inside function

萝らか妹 提交于 2019-11-26 17:26:05
My question is: function Foo() { this.foo = "bar"; // <- What is "this" here? } From what I can tell it depends on how Foo is used, i.e. as a constructor or as a function. What can this be in different circumstances? The this keyword refers to the object the function belongs to, or the window object if the function belongs to no object. It's used in OOP code, to refer to the class/object the function belongs to For example: function foo() { this.value = 'Hello, world'; this.bar = function() { alert(this.value); } } var inst = new foo(); inst.bar(); This alerts: Hello, world You can manipulate

What does “this” mean in a nodejs module?

Deadly 提交于 2019-11-26 17:25:25
问题 I have a simple codes like the following and execute it as a node module: console.log(this); module.exports = {…}; I know that global is the default context (like window in browsers), but what does the this keyword refer to? 回答1: this (in the context of a module) is the same as exports in node.js. However you should generally use exports / module.exports instead, so that it's explicitly clear what you're modifying. 来源: https://stackoverflow.com/questions/25058814/what-does-this-mean-in-a

Access “this” from Java anonymous class

﹥>﹥吖頭↗ 提交于 2019-11-26 17:18:49
Given the following code: public interface Selectable { public void select(); } public class Container implements Selectable { public void select() { ... } public void createAnonymousClass() { Selectable s = new Selectable() { public void select() { //see comment below. } }; } } I want to access Container.select() from within my anonymous class' select() method. However, this.select() would again call the anonymous class' select() method. My suggestion would be: Introduce a field into Container, e.g. private Container self = this; Now I can access Container.select() by calling self.select()