是否有类似的JavaScript函数从PHP压缩? [英] Is there a function in javascript similar to compact from php?

查看:140
本文介绍了是否有类似的JavaScript函数从PHP压缩?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现紧凑功能非常有用(在PHP)。这里是做什么的:

I found compact function very useful (in php). Here is what it does:

$some_var = 'value';
$ar = compact('some_var');
//now $ar is array('some_var' => 'value') 

因此​​,它创建变量,您可以指定,当元素的关键是变量名数组。
有什么样的功能在JavaScript?

So it create array from variables which you specify, when key for elements is variable name. Is there any kind of function in javascript ?

推荐答案

没有就没有类似的功能,也没有什么办法让变量名/值对当前上下文 - 只有当他们在全球变量窗口,这是不推荐使用。如果是这样,你可以这样做:

No there is no analogous function nor is there any way to get variable names/values for the current context -- only if they are "global" variables on window, which is not recommended. If they are, you could do this:

function compact() {
    var obj = {};
    Array.prototype.forEach.call(arguments, function (elem) {
        obj[elem] = window[elem];
    });
    return obj;
}

这篇关于是否有类似的JavaScript函数从PHP压缩?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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