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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:20:05+00:00 2026-05-24T16:20:05+00:00

In PHP we can do this: $variable = name_of_variable; $this->{$variable} = somevalue; how to

  • 0

In PHP we can do this:

$variable = "name_of_variable";
$this->{$variable} = "somevalue";

how to do this in javascript?

where use case should look like:

function Apple(){
    var name = "variable_name";
    this.(name) = "value";
}
console.log(new Apple());

to output

[Apple: {variable_name:"value"}]
  • 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-24T16:20:06+00:00Added an answer on May 24, 2026 at 4:20 pm

    try:

    this[name] = "value";
    

    All objects can use dot and array notation for variable access.

    Also note, this will allow you to create name value pairs that are inaccessible via dot notation:

    var foo = {};
    foo['bar-baz'] = 'fizzbuzz';
    alert(foo.bar-baz); //this will not work because `-` is subtraction
    alert(foo['bar-baz']); //this will work fine
    

    If you are creating a new object literal, you can use string literals for the names for values with special characters:

    var foo  = {'bar-baz':'fizzbuzz'};
    

    But you will not be able to use variables as the key within an object literal because they are interpreted as the name to use:

    var foo = 'fizz';
    var bar = { foo:'buzz' }
    alert( bar.fizz ); //this will not work because `foo` was the key provided
    alert( bar.foo ); //alerts 'buzz'
    

    Because other answerers are mentioning eval, I will explain a case where eval could be useful.

    Warning! Code using eval is evil, proceed with caution.

    If you need to use a variable with a dynamic name, and that variable does not exist on another object.

    It’s important to know that calling var foo in the global context attaches the new variable to the global object (typically window). In a closure, however, the variable created by var foo exists only within the context of the closure, and is not attached to any particular object.

    If you need a dynamic variable name within a closure it is better to use a container object:

    var container = {};
    container[foo] = 'bar';
    

    So with that all being said, if a dynamic variable name is required and a container object is not able to be used, eval can be used to create/access/modify a dynamic variable name.

    var evalString = ['var', variableName, '=', String.quote(variableValue), ';'].join(' ')
    eval( evalString );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an website with URL`s like example.com/index.php?p=home An this script $(document).ready(function() { var
In PHP and in JavaScript you can dynamically use a variable by using another
How can I this PHP function include in jquery .html? PHP function: function trim_text($string,
To grab the inode of a file in PHP, you can use this: $fs
I was wondering if PHP can do this as there seems to be no
I've noticed recently in PHP you can do this. $myNewArray = $oldArray + $someArray;
This is a bit hard for me to articulate, but in PHP you can
How can I do this Perl code in PHP? print unpack (H*, pack (B*,
I am using the SQL Server PHP Driver, I think this question can be
Can anyone help me with reversing this PHP page navigation? Current script setting shows

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.