I just wrote this piece of code to represent this error that is killing me (Grrr!)
I wonder why when I get error: method undefined I have checked in Safari and this variable inside parserDidStart() method is not of type EpisodeController it is of type EpisodeFeedParser why is this?
<html>
<head>
<script type="text/javascript">
var EpisodeFeedParser = function(url){
this.url = url;
this.didStartCallback = null;
};
EpisodeFeedParser.prototype.parse = function(doc){
this.didStartCallback(this);
};
var EpisodeController = function(){
this.episodes = new Array();
this.parser = null; //lazy
};
EpisodeController.prototype.parserDidStart = function(parser){
console.log("here *this* is not of type EpisodeController but it is EpisodeFeedParser Why?");
this.testEpi(); //**********ERROR HERE!***********
};
EpisodeController.prototype.fetchEpisodes = function(urlString){
if(urlString !== undefined){
if(parser === undefined){
var parser = new EpisodeFeedParser(urlString);
parser.didStartCallback = this.parserDidStart;
this.parser = parser;
}
this.parser.parse();
}
};
EpisodeController.prototype.testEpi = function(){
console.log("it worked!");
};
function testEpisode(){
var controller = new EpisodeController();
controller.fetchEpisodes("myurl");
}
</script>
</head>
<body>
<button type="button" onclick="testEpisode()">press me</button>
</body>
</html>
this is a frequently misunderstood aspect of Javascript. (and by “this”, I mean
this)You can think of
thisas another parameter that gets invisibly passed in to your functions. So when you write a function like,you’re really writing
That much is probably obvious, what isn’t obvious is exactly what gets passed in, and named as “this”. The rules for that are as follows. There are four ways to invoke a function, and they each bind a different thing to
this.classic function call
in the classic function call,
thisis bound to the global object. That rule is now universally seen as a mistake, and will probably be set to null in future versions.constructor invocation
in the constructor invocation,
thisis set to a fresh new object whose internal (and inaccessible) prototype pointer is set to add.prototypemethod invocation
in the method invocation,
thisgets set to someobject. it doesn’t matter where you originally defined add, whether it was inside a constructor, part of a particular object’s prototype, or whatever. If you invoke a function in this way,thisis set to whatever object you called it on. This is the rule you are running afoul of.call/apply invocation
in the call/apply invocation,
thisis set to whatever you pass in to the now visible first parameter of the call method.what happens in your code is this:
while you wrote parserDidStart with the expectation that its
thiswould be an EpisodeController when you method invoke it… what actually happens is you’re now changing itsthisfrom the EpisodeController to this.parser. That’s not happening in that particular line of code. The switch doesn’t physically happen until here:where
thisin this instance is the EpisodeParser, and by the time this code is run, you’ve asigned parserDidStart to be named didStartCallback. When you call didStartCallback here, with this code, you’re essentially saying…didStartCallback.call(this,this);
by saying this.didStartCallback() ,you’re setting its
thisto.. well.. thethiswhen you call it.You should be aware of a function called bind, which is explained here:
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
Bind creates a new function from an existing function, whose
thisis fixed (bound) to whatever object you explicitly pass in.