/    Sign up×
Community /Pin to ProfileBookmark

Hi – I am trying to untangle my confusion in relation to my understanding of the ‘this’ keyword in an example illustrating how to invoke overridden methods.
Ok here goes:

[LIST=1]

  • [*]

    As far as my understanding goes there is a rule in regards to when it occurs in functions, where ‘this’ refers to the global object

  • [*]

    and another rule when it occurs within a function that happens to be a method, in which case this refers to the object which invokes that method

  • [/LIST]

    [COLOR=”Red”]If that is the case then I would be grateful if someone can explain the following context in which they occur and are there additional rule(s) linked to them, because in the code below they occur in a function literal which would lead me to assume that ‘this’ refers to the ‘global object’ presumably but that clearly does not make sense in this case :[/COLOR]

    [CODE]PositionedRectangle.prototype.toString = function( ) {
    return “(” + [COLOR=”DarkGreen”]this[/COLOR].x + “,” + [COLOR=”#006400″]this[/COLOR].y + “) ” +
    [COLOR=”Purple”]this[/COLOR].superclass.prototype.toString.apply([COLOR=”Sienna”]this[/COLOR]);
    }
    [/CODE]

    [COLOR=”Red”]Another words can someone kindly explain to me what the two occurrences of ‘this’ mean and which rules apply to them, in above code sample.[/COLOR]

    to post a comment
    JavaScript

    1 Comments(s)

    Copy linkTweet thisAlerts:
    @mnaseersjauthorJun 10.2010 — I think the answer to this and please correct me if I am wrong in my assumption is that even though we are not talking about 'this' being used inside the body of a constructor function. But still it is in the context of an instance method of that object and therefore 'this' here refers to the Object instance that the instance method is being defined for.
    ×

    Success!

    Help @mnaseersj spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.28,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...