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

The Archive Base Latest Questions

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

I find this example in javascript design patterns and confused with the behavior of

  • 0

I find this example in “javascript design patterns” and confused with the behavior of following code

this code creates a self defining function:

var scareMe = function () {
   alert("Boo!");
   scareMe = function () {
     alert("Double boo!");
  };
};

now we are referencing it to another variable

var prank = scareMe; 

confusing part is when I called prank it should update scareMe and when i call it back it should alert “Double boo” isn’t it?

but the result is

prank(); // "Boo!"
prank(); // "Boo!"

and if I check scareMe function indeed it has been redefined.

scareMe(); // Double boo!

prank is just a reference to scareMe than why there is a difference?

  • 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:44:20+00:00Added an answer on June 9, 2026 at 4:44 pm

    prank points at the original function, not to scareMe.

    Look at this example:

    var scareMe = 1;
    var prank = scareMe;
    
    scareMe = 2;
    

    You don’t expect that changing scareMe would change prank, right? It’s exactly the same with functions.

    var scareMe = function() { alert( 'Boo' ); };
    var prank = scareMe;
    
    scareMe = function() { alert( 'Double boo!' ); };
    

    There is no difference between integers and functions in this regard—prank stays the same, even when scareMe changes. That scareMe is changed from inside another function does not change this fact.

    The confusion might come from how objects are usually used. It’s not as common to mutate the original function as you might change an object’s properties.

    var scareMe = { message: 'Boo' };
    var prank = scareMe;
    
    scareMe.message = 'Double boo!';
    
    alert( prank.message );  // alerts 'Double boo!'
    

    This is not what you’re doing with functions in the original example. Changing a variable to point to a completely different function does not change the function reference in the other variable.

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

Sidebar

Related Questions

Possible Duplicate: What does var that = this; mean in javascript? I often find
Greetings How do you find the deepest node? So for this example, String would
So I can't seem to find a good example of this anywhere. I found
all. Maybe i've not googled enough, but i can't find any example on this
I found this command line search and replace example: find . -type f -print0
I cannot find the solution for this problem, here is the simplified example: On
What does this do? I find the example here, but other than what it
Trying to make this jQuery filter that uses .find case-insensitive. For example, when the
Sorry if this has been asked before, but I couldn't find a good example
for example: this code: <asp:LinkButton ID=SignInLinkButton runat=server CssClass=SignInLinkButton TabIndex=3 OnClick=AuthorizeUser>SignIn</asp:LinkButton> shows this as link

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.