0

I try to bind a variable (this) to a whole class. Is this even possible without passing arguments from someEditFunction to anotherFunction?

I created a pen: https://codepen.io/anon/pen/vRBVRj

class Person {
    constructor(name) {
        this._name = name
    }

    entryPoint() {
        ClassTwo.someEditFunction.bind(this)()
    }

}

class ClassTwo {
    static someEditFunction() {
        console.log('called someEditFunction', this._name) // this._name is John
        ClassTwo.anotherFunction()
    }
    static anotherFunction() {
        console.log('called anotherFunction', this._name) // this._name is undefined
    }
}


let person1 = new Person('John', 15)
person1.entryPoint()
Jamiec
  • 133,658
  • 13
  • 134
  • 193
bonblow
  • 1,151
  • 6
  • 19
  • 28
  • 1
    No you can't bind it to the whole class. IMHO, this is a missus of the `this` keyword anyway. The `someEditFunction` and `anotherFunction` look like a helper function and as of that using `someEditFunction(obj) {obj._name}` seems to be more appropriate. – t.niese Mar 08 '18 at 08:22
  • 1
    As a note: `bind` is used if you want to bind the context (and arguments) to a function for later use. If you want to call a function with another context immediately you would use `call` or `apply` instead. So `ClassTwo.someEditFunction.bind(this)()` is not wrong, but you should use `ClassTwo.someEditFunction.call(this)` instead. – t.niese Mar 08 '18 at 08:26
  • Yes `call` and `apply` work with arguments: `ClassTwo.someEditFunction.call(this, 'arg 1', 'arg 2')` or `ClassTwo.someEditFunction.apply(this, ['arg 1', 'arg 2'])` – t.niese Mar 08 '18 at 08:30
  • Here is one issue with this... how long would you want ClassTwo to have its this binding set? – Travis J Mar 08 '18 at 08:30
  • Could it be you just mean to have `Person` class inherit from `ClassTwo`? – Icepickle Mar 08 '18 at 08:41
  • OOP is meant to be used in certain ways. `static` and `this` etc. have specific meanings, and you're supposed to use them in specific patterns to solve specific architectural problems. What you're trying here is *not* how these things are supposed to be used. – deceze Mar 08 '18 at 08:43
  • Why are you doing this in the first place? Just get rid of ClassTwo and the static functions. – rtn Mar 08 '18 at 22:26

1 Answers1

0

edit: I misread the question, disregard previous answer :)

Basically your problem is, static methods should not use any variable from the class. They should act on their own as utility methods. So you pass them variables doThing(a, b) rather than binding and abusing 'this' which just confuses everything.

If you want those methods to just use the instantiated object, i.e. be instance methods, then remove the static declaration and make them prototype methods. helpful docs

court3nay
  • 2,215
  • 1
  • 10
  • 15
  • Too bad this doesn't actually work, since `this.anotherFunction` is `undefined`. – deceze Mar 08 '18 at 08:41
  • assuming you want an 'instance variable'. if you want a 'class variable' that was specifically denied by the ES6 implementers for reasons listed [here](https://stackoverflow.com/questions/22528967/es6-class-variable-alternatives) – court3nay Mar 08 '18 at 08:42
  • There's the *Copy snippet to answer* button in the question which you could use to directly implement a working snippet here… – deceze Mar 08 '18 at 08:44