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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:58:16+00:00 2026-05-21T18:58:16+00:00

var obj = {x : 2}; eval.call(obj, ‘x’); // ReferenceError: x is not defined

  • 0

var obj = {x : 2};
eval.call(obj, ‘x’); // ReferenceError: x is not defined
How to change last line to get value by variable name?

Clarification
‘x’ – any expression in some obj context, possibly not a obj parameter

Original problem

/**
 * Guards 'this' when delegating method as a callback to others. Arguments aren't known beforehand and will be provided by consumer.
 */
delegate = function(context, func) {
    return function() {
        var args = [];
        for ( var i = 0; i < arguments.length; i++)
            args.push(arguments[i]);
        func.apply(context, args);
    };
};

reference = function (context, 'expression to evaluete to get reference value') {
    ... TODO
};

‘delegate’ – delegates function call, how to ‘delegate a reference’ with possibility to ‘dereference’ in different context?

Updates
here is 2 possibilities to use eval:
1. eval.call(null, ‘evaluate expression in global context’);
2. eval(‘evaluate expression in context where eval function was called’)
3. how to eval in some predefined ‘context’ ?

I want to mimic C++ reference when you can put it in any context and ‘dereference’ to value (but when in moment you dereference it value could be changed and you should get new value). Like in javascript you put object as function argument and anytime you get obj.someproperty you’ll get newest someproperty value (if it was changed). But this will not work when you will try to pass a primitive or what if whole object will be changed by some other. How to ‘pass a reference’ to ‘whole object’ to some other context?

Got clear understanding what I want 🙂
javascript how to create reference

What I was looking for:

createReference = function(context, prop) {
    return function() {
        return context[prop];
    };
};

Provider = function() {
};
Provider.prototype.x = 5;
Provider.prototype.getXRef = function() {
    return createReference(this, 'x');
};
Provider.prototype.incrementX = function() {
    this.x = this.x + 1;
};

var provider = new Provider();
var refX = provider.getXRef();
provider.incrementX();
alert(refX());

Tanks Brianpeiris!

  • 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-21T18:58:17+00:00Added an answer on May 21, 2026 at 6:58 pm

    This should do it, no need for eval:

    var obj = {x: 2};
    var value = obj['x'];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it like... var obj = new Object(); obj.function1 = function(){ //code } or
Is there an easier way to achieve the following? var obj = from row
var obj = { '51' : { 'name':'name1'}, '66' : { 'name':'name2'}, '58' :
Sometimes I'll see code like this: var Obj = Obj || {}; What does
var i : integer; i := 1234567; Given the above, I want the string
var y = new w(); var x = y.z; y.z= function() { doOneThing(); x();
var insInvoice = new NpgsqlCommand( @INSERT INTO invoice_detail( invoice_id, invoice_detail_id, product_id, qty, price, amount)
var foo = { someKey: someValue }; var bar = someKey; How do I
var newUser = new tblUser() { Email = strEmail, Password = strPassword, DateBirth =
Consider: var something = { wtf: null, omg: null }; My JavaScript knowledge is

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.