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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:57:08+00:00 2026-05-30T05:57:08+00:00

I’ve got a JavaScript object built like this: var Dashboard = { form: {

  • 0

I’ve got a JavaScript object built like this:

var Dashboard = {
  form: {
    action: function(){
      return $('#upload_form').attr('action');
    }(),
    //snip (more functions)
  }
}

If I call (using Chrome 17 on WinXP) Dashboard.form.action in the Chrome console after the page loaded (I checked the script and the function is there) the result is undefined but, if I then redefine Dashboard.form.action using the same function:

Dashboard.form.action = function(){
  return $('#upload_form').attr('action');
}();

and subsequently call it, it works as expected!

What am I doing wrong? Is this a bug or am I just overthinking it?

  • 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-30T05:57:11+00:00Added an answer on May 30, 2026 at 5:57 am

    Update:

    Re your comment below:

    actually what I want to do IS assigning the result to the action property…

    In the question you said:

    If I call…Dashboard.form.action…

    which makes it seem like you’re expecting action to be a function (you don’t “call” non-functions).

    If you’re expecting it to be a string (the “action” attribute value from #upload_form), then you don’t need to use a function at all. But you do need to be sure that you’re doing it after the #upload_form element already exists in the DOM.

    To do that, either put your script below it in the markup (anywhere below it is fine; just before or just after the closing </body> tag works well), or wrap your script in a ready call.

    So your code becomes either this if it’s after the #upload_form in the markup:

    var Dashboard = {
    
      form : {
          action : $('#upload_form').attr('action'),
    
          //snip (more functions)
      }
    };
    

    …or this if you want to use ready (anything else using Dashboard will have to wait until ready as well):

    jQuery(function($) {
        var Dashboard = {
    
          form : {
              action : $('#upload_form').attr('action'),
    
              //snip (more functions)
          }
        };
    });
    

    Note that in the second case, Dashboard will no longer be a global variable. That’s a good thing, but if you need it to be global for some reason, you can export it:

    jQuery(function($) {
        var Dashboard = {
    
          form : {
              action : $('#upload_form').attr('action'),
    
              //snip (more functions)
          }
        };
    
        // Export the global
        window.Dashboard = Dashboard;
    });
    

    Just make sure nothing tries to use Dashboard before ready has fired.


    Original answer:

    You have an extra pair of () on that:

    action: function(){return $('#upload_form').attr('action');}()
    //                                              here -------^^
    

    By putting them there, you call the function immediately, and assign the result of calling it to the action property. You just want to assign the function itself to the property, so don’t put the () at the end to call it:

    action: function(){return $('#upload_form').attr('action');}
    

    This is for exactly the same reason you wouldn’t use () here (let’s assume you have a function called foo) if you want f to refer to foo:

    var f = foo;
    

    If you said

    var f = foo();
    

    …you’d be calling foo, not referring to it.

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

Sidebar

Related Questions

I've got a JavaScript object, built this way: function foo() { this.length = 0;
I've got a JavaScript object similar to this: var profile1 = { one: "1",
Hallo all. I got a javascript object with some propeties let's say function Animal()
i got a question when create a javascript object, when one function invoking another
I got a javascript code like this to validate the file extension for my
I have got some javascript code and I'd like to convert this to C#.
I've got a javascript function I created. Somewhere in that function, this line is
I got this example demonstrating AJAX from Stoyan Stefanovs Object Oriented JavaScript at page
I got this javascript code from off the internet. It's a script that changes
I've got the following JavaScript on my web page... 64 var description = new

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.