Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4324744
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:10:25+00:00 2026-05-21T09:10:25+00:00

I just wrote this piece of code to represent this error that is killing

  • 0

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> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T09:10:26+00:00Added an answer on May 21, 2026 at 9:10 am

    this is a frequently misunderstood aspect of Javascript. (and by “this”, I mean this)

    You can think of this as another parameter that gets invisibly passed in to your functions. So when you write a function like,

    function add (a,b) {
       return a+b;
    }
    

    you’re really writing

    function add(this, a, b) {
        return a+b;
    }
    

    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

    add(a,b);
    

    in the classic function call, this is 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

    new add(a,b);
    

    in the constructor invocation, this is set to a fresh new object whose internal (and inaccessible) prototype pointer is set to add.prototype

    method invocation

    someobject.add(a,b);
    

    in the method invocation, this gets 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, this is set to whatever object you called it on. This is the rule you are running afoul of.

    call/apply invocation

     add.call(someobject,a,b);
    

    in the call/apply invocation, this is set to whatever you pass in to the now visible first parameter of the call method.

    what happens in your code is this:

     this.parser.didStartCallback = this.parserDidStart;
    

    while you wrote parserDidStart with the expectation that its this would be an EpisodeController when you method invoke it… what actually happens is you’re now changing its this from the EpisodeController to this.parser. That’s not happening in that particular line of code. The switch doesn’t physically happen until here:

    this.didStartCallback(this);
    

    where this in 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 this to.. well.. the this when 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 this is fixed (bound) to whatever object you explicitly pass in.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.