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

  • Home
  • SEARCH
  • 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 6820613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:26:44+00:00 2026-05-26T21:26:44+00:00

Possible Duplicate: Self-references in object literal declarations I have an object literal which is

  • 0

Possible Duplicate:
Self-references in object literal declarations

I have an object literal which is used as a configuration element and is looked up for keys.

customRendering:{
                  key1: func(){....},
                  key2: func(){....}
}

I have a situation where key2 and key3 can use the same function. Is there a way to assign the same function to both key2 and key3 without having to declare the function outside the object literal and without having to declare an extra key?

I was hoping I could do something like:

key2: key3: func(){....}
  • 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-26T21:26:45+00:00Added an answer on May 26, 2026 at 9:26 pm

    A safe way:

    I don’t know what I was thinking earlier. If you’re fine with using a more verbose “literal” you can instantiate a custom function:

    o = new function () {
      this.foo = function () { console.log('works'); };
      this.bar = this.foo;
    };
    

    This is a dirty nasty hack:

    you could use a temporary variable to store the reference to the function while setting the object. Be careful to use a closure and to call var before using it so that you don’t pollute the global namespace:

    (function () {
      var o, baz;
      o = {
        foo: baz = function () {console.log('works')},
        bar: baz
      }
      //more code
    }());
    

    The reason I’m calling this a dirty nasty hack is that it makes the code less readable, it’s harder to tell examining this code (especially if the object literal declaration was longer) where baz was set.

    Better to just write the alias outside the object literal so that it’s explicitly visible that it is an alias.

    Note: the named function format doesn’t work:

    o = { //THIS WON'T WORK
      foo: function baz() {/* code */},
      bar: baz
    }
    

    There’s no way within an object literal to define an alias using a shared reference.

    You can use an aliasing function, but it wont be an identical reference:

    o = {
      foo: function...
      bar: function () { return this.foo() } //even better if you use `apply` or `call`
    }
    

    The typical way to share a reference is after the object literal, which sounds like what you wanted to avoid:

    o = {
      foo: function...
    }
    o.bar = o.foo;
    

    Alternatively as you pointed out in your question (and for completeness) you could define the function outside of the object literal:

    func = function () {/* code */};
    o = {
      foo: func,
      bar: func
    }
    

    In response to @Peter about returning an object from a function

    Using a self-executing anonymous function is another way of instantiating an object inline, and would make this entire question moot:

    o = (function () {
      var o = {
        foo: function () {/*code*/}
      }
      o.bar = o.foo;
      return o;
    }());
    

    Or like this:

    var o = (function () {
      var shared = function() { console.log("shared") };
      return {
        foo: shared,
        bar: shared
      }
    }());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Self-references in object literal declarations Within a .js file I have an
Possible Duplicate: Self-references in object literal declarations How do I do the following: var
Possible Duplicate: class << self idiom in Ruby I have a quick Ruby question.
Possible Duplicate: RegEx match open tags except XHTML self-contained tags I have a HTML
Possible Duplicate: Should I Use self Keyword (Properties) In The Implementation? Say I have
Possible Duplicate: What website wireframing tools are available online, or self-hosted? I have a
Possible Duplicate: NSTimer doesn't stop I have this code: [NSTimer scheduledTimerWithTimeInterval:110.0 target:self selector:@selector(targetMethod:) userInfo:nil
Possible Duplicate: RegEx match open tags except XHTML self-contained tags i want to grap
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have

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.