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 6113365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:51:01+00:00 2026-05-23T14:51:01+00:00

I have the following code, which seems to override the jQuery.error function: $.test =

  • 0

I have the following code, which seems to override the jQuery.error function:

    $.test = function(){
        var test = this;

        test.error = function(){
            console.log('Why is this function getting called');
        };    
    };

    $(document).ready(function(){
        var someObj = $.test();
        $.error();
    });

This code will output ‘Why is this function getting called’ in the console.

However, if I change my code slightly, the native jQuery error function is called:

$(document).ready(function(){
    var someObj =  new $.test();
    $.error();
});

It seems that in the first example, ‘this’ refers to jQuery, but in the second example it refers to someObj. Why is this?

  • 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-23T14:51:02+00:00Added an answer on May 23, 2026 at 2:51 pm

    Your method $.test is a new property on the $ object. So it makes sense that the this object inside that method — the scope of the method — is object to which the property belongs.

    An example without jQuery (on fiddle here: http://jsfiddle.net/kxncT/):

    var a = {};
    
    a.method = function() {
        alert(this === a);  // true
    };
    
    a.method();
    

    However, when you call the new operator, you’re invoking a constructor. In that case, a new scope is created for the new object. So if in the above example you call new a.method(), the alert will read false, because the new scope that’s created is new, and unrelated to the parent object.

    In writing this answer, I’ve tried to paraphrase what I read in this blog post: http://javascriptweblog.wordpress.com/2010/08/30/understanding-javascripts-this/
    The post is much better than my answer, and if there are any discrepancies, trust the post 🙂

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

Sidebar

Related Questions

I have following code which seems to work fine: <%if group.employees.count > 0%> <td><%=
I have the following code which is seems to be lead to the infinite
I have the following code which seems to blow up if the column in
I have the following code which seems to work OK except for a minor
I have the following sample code which doesn't seem to want to run. import
I'm trying to use opengl in C#. I have following code which fails with
I have following Code Block Which I tried to optimize in the Optimized section
I have the following code which works just fine when the method is POST,
I have the following code which works fine. However, I only want to return
I have the following code which should put programs startable in Bash. if [

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.