this

When does the “fat arrow” (=>) bind to “this” instance

痴心易碎 提交于 2019-11-27 09:27:49
The fat arrow can be used in different settings but it somehow doesn't always bind to the instance I want. The fat arrow binds at 3 occasions when declaring a method when declaring a function within a method when declaring a function in global context 1. When declaring a method When the Coffeescript compiler encouters the following syntactical pattern within a class declaration class A somemethod: (paramlist) => This will yield the following code within the constructor of class A this.somemethod = __bind(this.somemethod, this); That is the definition for that instance is overwriting the

Passing “this” in java constructor

半世苍凉 提交于 2019-11-27 09:13:55
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 "looks". Passing this to another method/object from inside the constructor can be rather dangerous. Many

How does event handlers with arrow functions achieve context binding

限于喜欢 提交于 2019-11-27 09:08:36
I know about the general theory of this binding (function call site what matters, implicit, explicit binding, etc...) and the methods to solving the problem of this binding in React so it always points to what I want this to be (binding in constructor, arrow functions, etc), but I am struggling to get the inner mechanisms. Take a look at these two pieces of code: class demo extends React.component { goToStore(event) { console.log(this) } render() { <button onClick={(e) => this.goToStore(e)}>test</button> } } vs. class demo extends React.component { goToStore(event) { console.log(this) } render

Javascript OOP - lost this in asynchronous callback

荒凉一梦 提交于 2019-11-27 07:53:40
问题 I have problem which still bothers me on js oop - I'm sure I'm doing it bad, but I cant get how to do it right. For example, I have this code Auth.prototype.auth = function () { var request = new XMLHttpRequest(); request.open('GET', this.getAuthServerURL() + '/token', true); request.send(); request.onloadend = function () { var response = JSON.parse(request.responseText); console.log(response); if(response.result == 'found') { var token = response.token; this.setToken(token); this.isSigned =

Does the comma operator influence the execution context in Javascript?

…衆ロ難τιáo~ 提交于 2019-11-27 07:36:01
var a = 1; var b = { a : 2, c : function () { console.log(this.a); } }; b.c(); // logs 2 (b.c)(); // logs 2 (0, b.c)(); // logs 1 The first is understandable, for "this" is pointed to Object "b". But why does the second one log the same result? I thought "this" should be pointed to the global execution context. And the third one, it seems that the comma operator influences the execution context. You really have a nice corner case there! My take on it: the first is straightforward. Just a standard call. The '.' operator lets you call the function setting b as the execution context. the second

Any way to define ALL variables within function as property of this?

霸气de小男生 提交于 2019-11-27 07:25:05
问题 I know this may sound a little absurd, but I'm looking for a way to define every variable within a function as a property of this . I'm looking for any hack, any way possible to be able to have some way to track variables within a function (i.e. add them to the this object) without having to actually preface every single variable definition with this. . Is there a way? Is this possible with Proxy ? function () { // declare a variable var hello = 'hi' return this } let {hello} = function()

Typescript “this” inside a class method

与世无争的帅哥 提交于 2019-11-27 07:21:39
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 { requestAnimationFrame(this.updateProxy); //fine } } But coming from an Actionscript 3 background, I

PHP Fatal error: Cannot use $this as parameter

爱⌒轻易说出口 提交于 2019-11-27 07:21:32
问题 I've the following PHP method which is part of the codebase which was working fine: <?php class HooksTest extends DrupalTestCase { public function testPageAlterIsLoggedIn() { $this->drupal->shouldReceive('userIsLoggedIn') ->once() ->andReturn(TRUE); $this->drupal->shouldReceive('drupalPageIsCacheable') ->once() ->andReturnUsing(function ($this) { return $this; }); $page = []; $cacheable = $this->object->pageAlter($page); $this->assertFalse($cacheable); } } The code was passing all the CI

Recursive Constructor Invocation

爱⌒轻易说出口 提交于 2019-11-27 07:17:55
问题 public class LecturerInfo extends StaffInfo { private float salary; public LecturerInfo() { this(); this.Name = null; this.Address = null; this.salary=(float) 0.0; } public LecturerInfo(String nama, String alamat, float gaji) { super(nama, alamat); Name = nama; Address = alamat; salary = gaji; } @Override public void displayInfo() { System.out.println("Name :" +Name); System.out.println("Address :" +Address); System.out.println("Salary :" +salary); } } This code shows an error which is:

java this keyword

假装没事ソ 提交于 2019-11-27 07:15:36
问题 I have read that in Java you don't have to explicitly bind the this keyword to object, it is done by interpreter. It is opposite to Javascript where you always have to know the value of this . But where is this in Java is pointing - to class or object ? Or does it vary ? This question is a part of my attempt to understand basic OO concepts and design patterns so I can apply them to Javascript. Thank you. 回答1: The Java language specification states: When used as a primary expression, the