The this.nice(); code below, though you can invoke your non-instance methods that way, does not mean you should.
Most of the time, Function.apply.bind's second parameter is null for utility functions. Utility functions are those functions that belongs to class, not to the instance of the class.
The .nice won't get invoked when using .apply.bind. Worse yet, when using function-based class (pre-ES6), this points to global this, so if there's a global nice function, it would get called instead.
Though ES6 has first-class support for class, and null will be assigned to this when invoking .apply.bind on class's static method, thus preventing miscalled/hijacked function, it's not a good practice to invoke static (non-instance) methods from a this reference.
Just call the function directly from the parent function/class:
Do note that TypeScript transpilation won't assign null to this when using Function.apply.bind against the generated class. this still receives global this when .apply's first parameter is passed with null value.
The transpilation can't prevent miscalled/hijacked function.
Note that miscalled/hijacked function happens only on node's REPL or browser's console tool. It does not happen when invoked directly via node:
No comments:
Post a Comment