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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:52:11+00:00 2026-06-09T16:52:11+00:00

Given functions in JavaScript are a reference type (copied by reference, unlike simple types),

  • 0

Given functions in JavaScript are a reference type (copied by reference, unlike simple types), what precisely is going on here?

var func = function() { alert(1); };
var func_alias = func;
var func = function() { alert(2); };
func_alias(); //1

If func_alias is a reference to func, why is it non-updating? Wouldn’t you expect its invocation (line 4) to return 2?

This is different behaviour from other by-reference examples:

var obj = {prop: 'val'};
var obj_alias = obj;
obj.prop = 'updated val';
alert(obj_alias.prop); //updated val - not original one

func_alias seems to have kept a copy of the original, pre-overwrite func – in short, it seems to behave as though it’s copied it by value. This of course is disproven by:

var func = function(){}
var func_alias = func;
func === func_alias; //true
  • 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-06-09T16:52:13+00:00Added an answer on June 9, 2026 at 4:52 pm

    var func = function() { alert(1); };

    Here a new function is created and a reference to it is copied to func

    var func_alias = func;

    The function reference stored is func is assigned to another variable func_alias

    var func = function() { alert(2); };

    Another new function is created and a reference to it is assigned to func. The reference to the first function stored in func is lost. But since you saved it in func_alias before overwriting func, it can still be called.

    func_alias();

    func_alias was assigned a reference to the first function in step 2 (and wasn’t overwritten after that point). So the first function is called.


    EDIT #1

    As per your second example using an object:

    var obj = {prop: 'val'};
    

    Here, a new object is created and a reference to it is assigned to obj

    var obj_alias = obj;
    

    The reference is copied to another variable obj_alias

    obj.prop = 'updated val';
    

    Here you are not overwriting the value of obj, but only overwriting a property of the object pointed to by that reference stored in obj. The value of obj (i.e. reference to the object created in first step) remains intact.

    In your function example, you were actually overwriting the variable func with a new function reference.

    alert(obj_alias.prop);
    

    The value of obj_alias as well as that of obj is still the same, since you haven’t overwritten either. They both hold a reference to the object created in step 1.


    EDIT #2

    This can maybe be explained well in C terms.

    • When you create an object via var obj = {prop: 'val'}; – lets say the object is stored at address 0x0001 in memory. i.e. the actual value of obj is 0x0001

    • When you assign it to obj_alias, obj_alias also gets the value 0x0001 – now both variables point to something stored at address 0x0001

    • When you do obj.x = y, you aren’t overwriting the value of obj, but only using obj to access the object stored at 0x0001 and modifying one of its properties.

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

Sidebar

Related Questions

Given this HTML: <form id=formx> <input type=text value= /> </form> And this JavaScript: $(function()
Given this plain JavaScript construct: var MyObject = function() { var privateArray = [
Given the following javascript (jquery) $(#username).keyup(function () { selected.username = $(#username).val(); var url =
Here's some JavaScript: linkElem.click(function () { var data = linkElem.data(); alert(''+data.mls + ' '
Given the following snippet of javascript in a scope: var x = 10; function
stated here https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function the constructor property of an instance of a function object specifies
Given the following javascript: function foo(selectControl) { ... } and the following html: <select
Using the javascript function function squareIt(number) { return number * number; } When given
Given remote page: http://example.com/paged_list.aspx which uses a Javascript function call to display several pages
I have a JavaScript file which has a function that I reference. When I

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.