Created
June 28, 2011 15:21
-
-
Save mgile/1051375 to your computer and use it in GitHub Desktop.
callbacks from within a Javascript "class"
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 MyClass = function() { | |
this.myObject = {}; | |
this.name = "MyClass"; | |
}; | |
/** | |
* doSomethingWithFileData | |
* @param err | |
* @param fileData | |
*/ | |
MyClass.prototype.doSomethingWithFileData = function(err, fileData) { | |
if(typeof this.myObject !== 'object' && this.name !== "MyClass") { | |
console.log("You fail! Scoping error, 'this' is not the 'this' you are looking for."); | |
console.log("'this': " + this); | |
console.log("\n\n"); | |
} | |
else { | |
console.log("You win! 'this' is: " + this.name); | |
console.log("Here is your data: \n" + fileData); | |
console.log("\n\n"); | |
} | |
}; | |
MyClass.prototype.readFileAsync = function(filePath) { | |
// Wrong | |
fs.readFile(filePath, this.doSomethingWithFileData); | |
// Right | |
fs.readFile(filePath, CALLBACK(this, 'doSomethingWithFileData')); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment