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

  • SEARCH
  • Home
  • 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 6890731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:19:11+00:00 2026-05-27T06:19:11+00:00

I´m littel confused with this reserve key and cases caused errors. Here a sample

  • 0

I´m littel confused with this reserve key and cases caused errors. Here a sample code where this causes errors.

var sample = {
    init: function() {
        this.sampleFunction0();
        this.sampleFunction1();
    },
    sampleFunction0 : function(){
        var something0, something1, something2;
        something0 = this.sampleFunction2(); // works, there is no ambiguity whit 'this'
        jQuery('#list li').click(function(){
            something1 = this.sampleFunction2(); // ambiguity: not works, but sample.sampleFunction2(); works
            something1 = sample.sampleFunction2(); // it´s works
            something2 = $(this).text(); // list item val
            console.log(something0) // something
            console.log(something1); // something : using sample.sampleFunction2();
            console.log(something2); // item val
        });
    },
    sampleFunction1 : function(){
        return 'someting';
    },
    sampleFunction2 : function(){
        return 'something';
    }
}

jQuery(document).ready(function(){
    sample.init();
});

I don´t know if is correct to use sample.sampleFunction2(); instead this.sampleFunction2();

  • 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-27T06:19:12+00:00Added an answer on May 27, 2026 at 6:19 am

    From your question I think you probably know this, but: Within the event handler, this refers to the element on which you hooked the event, which is of course why this.sampleFunction2() doesn’t work. So this will be the li element that was clicked — which is frequently quite handy.

    In your specific case, using sample rather than this is fine, because your sample object is a one-off, there won’t be more than one of it. So yes, just use sample.sampleFunction2(); instead of this.sampleFunction2();.

    In cases where there may be more than one (things created via a constructor function, etc.), the typical thing to do in this case (no pun) is to define a local variable in your closure (your function you’re passing into click) can close over:

    sampleFunction0 : function(){
        var something0, something1, something2, self = this; // <==== CHANGE
        something0 = this.sampleFunction2(); // works, there is no ambiguity whit 'this'
        jQuery('#list li').click(function(){
            something1 = self.sampleFunction2(); // <==== CHANGE
            something2 = $(this).text(); // list item val
        });
    },
    

    There are times when it’s useful to use jQuery’s proxy function, but not in this case, because A) you’re already defining a closure, and B) you’re using this for something else. proxy is useful when you’re trying to bind a function that you’ve defined elsewhere, and when you need this to have a specific value other than the one it would normally have.

    More reading:

    • You must remember this
    • Mythical methods
    • Closures are not complicated
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a little confused by this snippet of code (presented in the CocoaFundamentals
I am a little confused here. I would like to do something like this:
I am a little confused on how prepend acts towards a children function. image.canvas.children('.image-pinpoint-view').prepend(this.area);
I am a little confused by this concept. If I override a public function
Okay, I am a little confused because this code was working and for some
So I'm a little confused by this terminology. Everyone refers to Asynchronous computing as
I am a little confused on the logic of how to write this SQL
I am a little confused how jQuery stores data with .data() functions. Is this
I'm not sure how to word this cause I am a little confused at
I'm a little confused by some PHP syntax I've come across. Here is an

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.