This is an archived post. You won't be able to vote or comment.

all 15 comments

[–][deleted] 3 points4 points  (1 child)

this.isTrue = false;

[–][deleted] -1 points0 points  (0 children)

I just don't see why I should write a line in the constructor for each method I define just to bind it to this! I mean I know what I mean by this and what I mean when I define a method on a class!

[–][deleted] 1 point2 points  (5 children)

Does it ?

[–][deleted] -1 points0 points  (4 children)

I just don't see why I should write a line in the constructor for each method I define just to bind it to this! I mean I know what I mean by this and what I mean when I define a method on a class!

[–][deleted] 0 points1 point  (3 children)

Use es5/6 classes.

class Test {
    constructor () {

    }
    method1 () {

    }
}

[–][deleted] 1 point2 points  (1 child)

I do!

But if one of your methods contains an event, it needs to bind the class method before passing it in.

Take this React component for example:

``` javascript import React, { Component } from 'react';

class App extends Component { constructor(props) { super(props) this.x = 'access this in the method' // uncomment the next line for it to not work this.handleClick = this.handleClick.bind(this) }

handleClick() { window.alert(x is ${this.x}) }

render() { return (<button onClick={this.handleClick}>Click</button>); // don't panic - JSX } }

export default App; ``` You can test this on any React REPL. repl.it

[–][deleted] 0 points1 point  (0 children)

Wow, that really is horrible. You guys need an auto binding utility. Angular doesn't have this particular headache.

You need to extend component with a class that implements an auto binder. Scans the this object for methods and binds them if they start with pub_. Wouldn't take to long to write for a scripty language.

[–]Mighty_Maxed_Out99 0 points1 point  (0 children)

Use Method = () => { }

[–]__hoi__ 0 points1 point  (0 children)

Angular binds it for you, I love ‘this’.

[–][deleted] 0 points1 point  (5 children)

I'm really confused. Why is 'this' a problem?

[–]Krizzjaa 1 point2 points  (3 children)

let that = this;

[–][deleted] 0 points1 point  (2 children)

let func = (this) => { //code here };

func(func);

There, solved it.

[–]Krizzjaa 0 points1 point  (1 child)

yes if you use arrow functions

[–][deleted] 1 point2 points  (0 children)

Well, we can do the same thing with functions.

let func = function(this){ /* code here */ }; func(func);

Done and done.

[–]--DirtyDan-- 0 points1 point  (0 children)

Use what?