PHP提取函数
<?php
$a = "Original";
$my_array = array("a" => "Cat","b" => "Dog", "c" => "Horse");
extract($my_array);
echo $a; // this will print Cat
?>
Run Code Online (Sandbox Code Playgroud)
同样,我想访问 javascript 对象键作为变量
var myObject = {a: 'cat', b: 'dog'}
// If console 'a' then it should print Cat
Run Code Online (Sandbox Code Playgroud)
我尝试使用thisandeval但它只能在浏览器中工作,这在 NodeJs 中怎么可能?
评估
var k = { a: 'a0', b: 'b0' }
Object.keys(k).forEach((key) => {
eval(`${key}=k[key]`);
console.log(a)
});
Run Code Online (Sandbox Code Playgroud)
这
function convertKeyToVariable(data, where) {
for (var key in data) {
where[key] = data[key]; …Run Code Online (Sandbox Code Playgroud)