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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:17:58+00:00 2026-06-04T00:17:58+00:00

I have an object in my class like so: this.options = { a: 1,

  • 0

I have an object in my class like so:

this.options = {
     a: 1,
     b: 2,
     ...
};

I just wondered if there was a way to “attach” the options object to the current scope in Javascript, such that I can refer to a and b directly (instead of this.options.a and this.options.b). Something like:

attach(this.options);
var myVariable = a + 3; // 4
detach(this.options);

or

with( this.options, {
   // code here
 var myVariable = a + 3; // 4
});

In the above, the attach and with have the effect of taking all of the children of this.options and attaching them to the current scope so they are accessible via a instead of this.options.a. (The with version simply detaches this.options once you’re done, enclosing the potentially-damaging attaching to within that scope).

As long as it works in the Spidermonkey/Mozilla engine, I don’t mind if it’s not portable.

I only mention this because of curiosity & laziness: all the nested names are getting annoying to type out, and I know that in the R language this is possible (in fact, the syntax in the snippets above is almost exactly what you’d write in R to achieve this).

  • 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-04T00:17:59+00:00Added an answer on June 4, 2026 at 12:17 am

    JavaScript has a with statement that works similar to what you describe:

    with (this.options) {
        var myVariable = a + 3;  // here a is actually this.options.a
    }
    

    However as you can see at the MDN article I linked to there are some cons to using it, because, e.g., of potential ambiguity. In the example above, a could be a property of this.options, or it could be a variable defined elsewhere, and looking just at that block there is no way to tell.

    with is not recommended (and is forbidden in strict mode).

    There is a safe alternative though: create a variable that references (again in your example) this.options:

    var o = this.options;
    
    var myVariable = o.a + 3;
    

    It’s not quite as short as typing a on its own, but it’s shorter than repeating this.options everywhere.

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

Sidebar

Related Questions

I'm just wondering .. I have and object like this public class Entry{ public
Let's say I have a class like this: class Test(object): prop = property(lambda self:
I have an object which looks something like this class MyObject { string Name;
I have a singleton implemented like this: class Test123(object): _instance = None def __new__(cls,
I have an object hierarchy that looks something like this: public class Book {
I would like to have the form object like this: public class FormData {
This is a best practices question Let say, I have a class object, like
So, here's my problem. I have objects like this public class FooClass{ private int
I want to have immutable Java objects like this (strongly simplified): class Immutable {
I have a model like this class Parent < ActiveRecord::Base :has_many :kids end class

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.