Skip to content

Instantly share code, notes, and snippets.

@dschenkelman
Created May 9, 2014 04:46
Show Gist options
  • Save dschenkelman/18841f8d7d2b08ab0e92 to your computer and use it in GitHub Desktop.
Save dschenkelman/18841f8d7d2b08ab0e92 to your computer and use it in GitHub Desktop.
To Arrow Function or not to Arrow Function
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;
})();
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);
}
}
}
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