-
-
Save mxriverlynn/28a4072ec51ba71d9556 to your computer and use it in GitHub Desktop.
when "this" attacks with ES6 arrow functions
This file contains 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
FileSchema.method("getDownloadUrl", (cb) => { | |
var options = { | |
Bucket: "watchmecode-net", | |
Key: this.name // <= "this" right here, points at the wrong thing!!! | |
}; | |
var s3 = new AWS.S3(); | |
s3.getSignedUrl("getObject", options, cb); | |
}) |
This file contains 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 foo = {}; | |
// a method to create methods | |
foo.method = function(name, cb){ | |
this[name] = cb; | |
}; |
This file contains 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
foo.method("bar", function(){ | |
console.log("test"); | |
} | |
foo.bar(); // => "test" |
This file contains 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
// run this in node v4 to see the "expected" behavior | |
this.test = "attached to the module"; | |
var foo = { | |
test: "attached to an object" | |
}; | |
// a method to create methods | |
foo.method = function(name, cb){ | |
this[name] = cb; | |
}; | |
// use an arrow function and get | |
// lexical analysis of "this" | |
foo.method("bar", () => { | |
// not what you expected, maybe? | |
console.log(this.test); | |
}); | |
foo.bar(); |
This file contains 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
// run this in node v4 to see the expected behavior | |
this.test = "attached to the module"; | |
var foo = { | |
test: "attached to an object" | |
}; | |
// a method to create methods | |
foo.method = function(name, cb){ | |
this[name] = cb; | |
}; | |
// use standard callback function and get | |
// the expected "this" | |
foo.method("bar", function(){ | |
// this one will be what you expected | |
console.log(this.test); | |
}); | |
foo.bar(); // => "attached to an object" |
This file contains 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
// run this in node v4 to see the "expected" behavior | |
this.test = "attached to the module"; | |
var foo = { | |
test: "attached to an object" | |
}; | |
foo.method = function(name, cb){ | |
// bind the value of "this" on the method | |
// to try and force it to be what you want | |
this[name] = cb.bind(this); | |
}; | |
foo.method("bar", () => { | |
console.log(this.test); | |
}); | |
foo.bar(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is the following error in 3.js -
Uncaught SyntaxError: missing ) after argument list