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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:54:19+00:00 2026-05-16T14:54:19+00:00

I thought this would be something I could easily google, but maybe I’m not

  • 0

I thought this would be something I could easily google, but maybe I’m not asking the right question…

How do I set whatever “this” refers to in a given javascript function?

for example, like with most of jQuery’s functions such as:

$(selector).each(function() {
   //$(this) gives me access to whatever selector we're on
});

How do I write/call my own standalone functions that have an appropriate “this” reference when called? I use jQuery, so if there’s a jQuery-specific way of doing it, that’d be ideal.

  • 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-16T14:54:19+00:00Added an answer on May 16, 2026 at 2:54 pm

    Javascripts .call() and .apply() methods allow you to set the context for a function.

    var myfunc = function(){
        alert(this.name);
    };
    
    var obj_a = {
        name:  "FOO"
    };
    
    var obj_b = {
        name:  "BAR!!"
    };
    

    Now you can call:

    myfunc.call(obj_a);
    

    Which would alert FOO. The other way around, passing obj_b would alert BAR!!. The difference between .call() and .apply() is that .call() takes a comma separated list if you’re passing arguments to your function and .apply() needs an array.

    myfunc.call(obj_a, 1, 2, 3);
    myfunc.apply(obj_a, [1, 2, 3]);
    

    Therefore, you can easily write a function hook by using the apply() method. For instance, we want to add a feature to jQuerys .css() method. We can store the original function reference, overwrite the function with custom code and call the stored function.

    var _css = $.fn.css;
    $.fn.css = function(){
       alert('hooked!');
       _css.apply(this, arguments);
    };
    

    Since the magic arguments object is an array like object, we can just pass it to apply(). That way we guarantee, that all parameters are passed through to the original function.

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

Sidebar

Related Questions

I read this question and thought that would easily be solved (not that it
I thought this would be fairly simple but it turns out not to work
Before I throw something ugly together I thought this would be a good one
I thought this would be fairly easy, but I'm totally baffled. I want one
I thought this would be pretty easy but I'm running into all sorts of
I thought this would be easy. The Google makes me think otherwise. What I
Okay at first I thought this would be pretty straightforward. But I can't think
OK so I thought this would be pretty simple but it's tripping me up..
Ok, I thought this would be simpler but I was probably wrong.. My parser
I've been reading through regex and I thought this would work but it doesn't

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.