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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:49:32+00:00 2026-06-04T08:49:32+00:00

Is it problematic to reference an object literal within a function which is part

  • 0

Is it problematic to reference an object literal within a function which is part of that very literal? It seems to work just fine, but I want to make sure there aren’t other implications.

Here’s an example of what I’m talking about:

instead of:

var obj = {
    key1: "it",
    key2: function(){return this.key1 + " works!"}
};
alert(obj.key2());

using:

var obj = {
    key1: "it",
    key2: function(){return obj.key1 + " works!"}
};
alert(obj.key2());
  • 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-04T08:49:34+00:00Added an answer on June 4, 2026 at 8:49 am

    Both can be problematic.

    var obj = {
        key1: "it",
        key2: function(){ return this.key1 + " works!" }
    };
    var func = obj.key2;
    alert(func()); // error
    

    When func is not called as a method of obj, this can reference something else (in here: the global object “window“).

    var obj = {
        key1: "it",
        key2: function(){ return obj.key1 + " works!" }
    };
    var newref = obj;
    obj = { key1: "something else"; };
    alert(newref.key2()); // "something else works"
    

    In here we access the object from another reference, though the obj in the function may now point to some other object.

    So you will have to choose which case is more likely. If you really want to make it safe, prevent obj from being exchanged:

    // ES6 - use `const`:
    const obj = {
        key1: "it",
        key2: function(){ return obj.key1 + " works always!" }
    };
    
    // ES5: use a closure where the `obj` is stored in a local-scoped variable:
    var obj = (function(){
        var local = {
            key1: "it",
            key2: function(){ return local.key1 + " works always!" }
        };
        return local;
    })();
    

    or you bind() the function to the object:

    var obj = {
        key1: "it",
        key2: function(){ return this.key1 + " works always!" }
    }
    obj.key2 = obj.key2.bind(obj);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have just completed implementing a successful but problematic project. We want to get
I read that self closing tags were problematic for some browsers such as IE7
The example below may not be problematic as is, but it should be enough
It seems that Python has some limitations regarding instance methods. Instance methods can't be
This error seems to be posted all over the place but each one seems
I have two Document Models that are proving problematic: class Component include Mongoid::Document include
the following code is being problematic. <script type=text/javascript src=<javascript/prototype.js></script> <script type=text/javascript> // <![CDATA[ $(document).ready(function
I'd like to create a function which can cope with JSON in the following
I'm coming across a problematic leak which - thanks to Instruments - appears to
These rewrite rules don't seem to work. Is it just not possible or am

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.