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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:14:42+00:00 2026-05-14T21:14:42+00:00

In Javascript, if we are aliasing a function (or, assign a reference to a

  • 0

In Javascript, if we are aliasing a function (or, assign a “reference to a function” to another variable), such as in:

f = g;
f = obj.display;
obj.f = foo;

all the 3 lines above, they will work as long as the function / method on the right hand side doesn’t touch this? Since we are passing in all the arguments, the only way it can mess up is when the function / method on the right uses this?

Actually, line 1 is probably ok if g is also a property of window? If g is referencing obj.display, then the same problem is there.

In line 2, when obj.display touches this, it is to mean the obj, but when f is invoked, the this is window, so they are different.

In line 3, it is the same: when f is invoked inside of obj‘s code, then the this is obj, while foo might be using this to refer to window if it was a property of window. (global function).

So line 2 can be written as

f = function() { obj.display.apply(obj, arguments) }

and line 3:

obj.f = function() { foo.apply(window, arguments) }

Is this the correct method? And are there other methods besides 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-14T21:14:43+00:00Added an answer on May 14, 2026 at 9:14 pm

    Basically yes, however, in JavaScript functions are first class objects, so there it is not really aliasing. What you are doing is assigning the value of a variable to another variable, an in this case the value just happens to be a function.

    The magic this variable however is a little bit different. In JavaScript methods are not bound to a class or an object, like they are in most other languages. When you call a method the this is set by the dot operator, or by the apply or call methods. It works like this:

    var i = 1;
    var f = function () { alert(this.i); }
    f(); // in a browser, alerts 1, because this will be window if it isn't anything else.
    
    var obj = { i: 10 };
    obj.g = f;
    obj.g(); // alerts 10, because the dot binds this to obj
    
    var other_obj = { i: 23 };
    f.apply(g); // alerts 23, because now apply binds this to other_obj
    

    You can bind methods to objects by using closures:

    var f = function () { alert(this.i); }
    var obj = { i: 10 };
    
    obj.m = function () { f.apply(obj); }
    obj.m(); // alerts 10
    
    var g = obj.m;
    g(); // alerts 10, because now the closure is called, which binds this correctly.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 471k
  • Answers 471k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You need to extract the desired values first and then… May 16, 2026 at 3:25 am
  • Editorial Team
    Editorial Team added an answer A good principle when performing searches accross very large collections… May 16, 2026 at 3:25 am
  • Editorial Team
    Editorial Team added an answer You can extend javascript objects and add methods to it:… May 16, 2026 at 3:25 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.