Created
May 9, 2014 04:46
-
-
Save dschenkelman/18841f8d7d2b08ab0e92 to your computer and use it in GitHub Desktop.
To Arrow Function or not to Arrow Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var MyClass = (function () { | |
function MyClass() { | |
} | |
MyClass.prototype.add = function (a, b) { | |
return a + b; | |
}; | |
MyClass.prototype.partialAdd = function (a) { | |
var _this = this; | |
return function (b) { | |
return _this.add(a, b); | |
} | |
}; | |
MyClass.prototype.partialAdd2 = function (a) { | |
return function (b) { | |
return this.add(a, b); | |
} | |
}; | |
return MyClass; | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyClass { | |
constructor() { | |
} | |
private add(a, b) { | |
return a + b; | |
} | |
public partialAdd(a) { | |
return (b) => { | |
return this.add(a, b); | |
} | |
} | |
public partialAdd2(a) { | |
return function(b) { | |
return this.add(a, b); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyClass { | |
constructor() { | |
} | |
private add(a, b) { | |
return a + b; | |
} | |
public partialAdd(a) { | |
var that = this; | |
return function(b) { | |
// this is accessible inside the function | |
// and points to the object to which the function was applied | |
var _this = this; | |
// that is accessible inside the function through the closure | |
// and points to the object that had partialAdd called | |
return that.add(a, b); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment