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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:15:30+00:00 2026-05-10T17:15:30+00:00

JavaScript allows functions to be treated as objects–if you first define a variable as

  • 0

JavaScript allows functions to be treated as objects–if you first define a variable as a function, you can subsequently add properties to that function. How do you do the reverse, and add a function to an ‘object’?

This works:

var foo = function() { return 1; }; foo.baz = 'qqqq'; 

At this point, foo() calls the function, and foo.baz has the value ‘qqqq’.

However, if you do the property assignment part first, how do you subsequently assign a function to the variable?

var bar = { baz: 'qqqq' }; 

What can I do now to arrange for bar.baz to have the value ‘qqqq’ and bar() to call the function?

  • 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. 2026-05-10T17:15:31+00:00Added an answer on May 10, 2026 at 5:15 pm

    You can’t (as far as I know) do what you’re asking, but hopefully this will clear up any confusion.

    First, most objects in Javascript inherit from the Object prototype.

    // these do the same thing var foo = new Object(); var bar = {};  console.log(foo instanceof Object); // true console.log(bar instanceof Object); // true 

    Second, functions ARE objects in Javascript. Specifically, they are objects that inherit from the Function prototype, which itself inherits from the Object prototype. Check out Function on MDN for more information.

    // various ways of creating functions var foo = new Function(); var bar = function(){}; function baz(){};  // all functions are Function objects, and therefore Object objects console.log(foo instanceof Function); // true console.log(foo instanceof Object);   // true 

    If you create a value that is an Object object first, you can’t (as far as I know) convert it to a Function object later. What you CAN do however is create a new function, copy the properties from your old object to this function, and then reassign that function to the same variable, discarding the old object.

    You can quickly copy the properties of one object to another with Object.assign(target, source).

    var foo = { baz: "qqqq" };  var bar = function () { return 1; };  // create a new function Object.assign(bar, foo);              // copy the properties of foo to it foo = bar;                            // assign the function to foo  console.log(foo instanceof Function); // true console.log(foo());                   // 1 console.log(foo.baz);                 // "qqqq" 

    But this isn’t something you would typically do in practice. It’s generally not recommended to add properties to a standard object like functions in the first place.

    Finally, anticipating a possible question, you can’t change the body of a function after it has been created. The function body is stored internally and not exposed as a property.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I am not sure. I always use an import statement… May 11, 2026 at 11:45 pm
  • Editorial Team
    Editorial Team added an answer Can the "users" visit the site you're redirecting to by… May 11, 2026 at 11:45 pm
  • Editorial Team
    Editorial Team added an answer On my Linux system (Ubuntu Jaunty), I have Python 2.5,… May 11, 2026 at 11:45 pm

Related Questions

JavaScript allows functions to be treated as objects--if you first define a variable as
What's the best way to pass an optional argument that is nearly always the
I'm looking for a framework which allows me to manage my application through a
I'm working on a calendar page that allows a user to click on a
This question deals with concurrency issues, for suggestions on how to display a busy

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.