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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:19:53+00:00 2026-05-30T21:19:53+00:00

In the following javascript code: function foo() { foo.val = foo.val || ‘no val’;

  • 0

In the following javascript code:

function foo() {
  foo.val =  foo.val || 'no val';
  return 'foo has ' + foo.val;
};
function bar() {
  bar.val =  bar.val || 'no val';
  return 'bar has ' + bar.val;
};
var a = foo;
foo.val = '1';
bar.val = '2';
a.val = '3';
foo = bar;
'foo says "' + foo() + '", bar says "' + bar() + '", a says "' + a() +'"';

what I would expect would be:

foo says “bar has 2”, bar says “bar has 2”, a says “foo has 3”

However, when run from the Firebug console in Firefox 10.0.2 I get:

foo says “bar has 2”, bar says “bar has 2”, a says “foo has 2”

Can anyone explain to me the sequence of events that goes on behind the scenes to make this so? Why does a stay bound to the original foo function (as I would expect) but hold bar‘s value for val?

  • 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-30T21:19:55+00:00Added an answer on May 30, 2026 at 9:19 pm

    After this line of code:

    var a = foo;
    

    a points to the same function that foo points to. In

    foo = bar;
    

    you reassign foo to point to whatever bar refers to. This doesn’t update a‘s reference — it still points to the function that foo also originally pointed to.

    Now, when you run a(), the original function is executed. It grabs foo (which now points to bar‘s reference) and gets its val property. The val of bar‘s object is 2, so this is what is returned.

    It’s a bit difficult to explain in words.. would a diagram be easier to understand, perhaps?

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

Sidebar

Related Questions

Consider the following JavaScript code which has a recursive boom function function foo() {
I have the following (simplified) code: var Foo = (function () { var data
Recently I saw the following code that creates a class in javascript: var Model.Foo
I have the following JavaScript code: (function() { function f(){ alert(1); } return f();
I found following in javascript code function calculateURI(uri) { return (namespace.push.configuration.uriPrefix || '') +
I'm using the following javascript code to display xml/xsl: function loadXMLDoc(fname) { var xmlDoc;
I'm currently doing the following to give my javascript code a namespace: (function(foo, $,
I have the following JavaScript code: Link In which the function makewindows does not
I have the following Javascript code add_num = { f: function(html, num) { alert(this.page);
I'm using the following JavaScript code: <script language=JavaScript1.2 type=text/javascript> function CreateBookmarkLink(title, url) { 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.