JavaScript中的变量变量 [英] Variable variables in JavaScript

查看:64
本文介绍了JavaScript中的变量变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,此功能已经存在于PHP中.让我们看下面的php代码:

according to my knowledge, this feature already exists in PHP. lets look at the following php code:

$color = 'red';
$$color = 'dark';

功能说明:

有时候,拥有可变变量名很方便.即,可以动态设置和使用的变量名.变量变量获取变量的值,并将其视为变量的名称.在上面的示例中, red 可以用作变量的名称.此时,已经定义了两个变量并将其存储在PHP符号树中:$color,其内容为"red"$red,内容为"dark".

description of the feature:

Sometimes it is convenient to be able to have variable variable names. That is, a variable name which can be set and used dynamically.A variable variable takes the value of a variable and treats that as the name of a variable. In the above example, red, can be used as the name of a variable.At this point two variables have been defined and stored in the PHP symbol tree: $color with contents "red" and $red with contents "dark".

这可以在Java脚本中完成吗?

can this be done in java Script?

推荐答案

var color = 'red';
window[color] = 'dark';
console.log(color, red);

这篇关于JavaScript中的变量变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆