javascript:获取所有对象参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4856387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:06:55  来源:igfitidea点击:

javascript: get all object parameters

javascriptobjectparametersparameter-passing

提问by Sorin Buturugeanu

I have a JS object with a variable number of parameters. Is there a way to see what parameters have been passed this particular time?

我有一个带有可变数量参数的 JS 对象。有没有办法查看这个特定时间传递了哪些参数?

The example:

这个例子:

function getElement() {
    var scope = document;

    this.by = function(data){
        if (data.id)    scope = scope.getElementById(data.id);
        if (data.tag)   scope = scope.getElementsByTagName(data.tag);       
        return scope;
    }
}

And I run it like so

我像这样运行它

var x = new getElement(); 
vad div = x.by({id : "chosenID"});

gets the div with the id chosenID

获取带有 id 的 div chosenID

or

或者

var x = new getElement(); 
vad inputs = x.by({id : "chosenID", tag : "input"});

gets all the inputsin the div with the id chosenID;

inputs使用 id获取div 中的所有内容chosenID

I want to know if I passed one or two parameters, and which ones.

我想知道我是否传递了一两个参数,以及哪些参数。

Thanks!

谢谢!

ps: I appreciate your time in helping me out, but please do not sugget jQuery or other JS framework as this is for learning purposes only. Much obliged, Sorin.

ps:感谢您花时间帮助我,但请不要建议使用 jQuery 或其他 JS 框架,因为这仅用于学习目的。非常感谢,索林。

回答by Marcel Korpel

Use a for … inloop to iterate over the passed object's parameters, like:

使用for … in循环遍历传递对象的参数,例如:

var prop;

for (prop in data) {
    if (data.hasOwnProperty(prop)) {
        // do something with data[prop]
    }
}

Don't forget to check the property with hasOwnProperty.

不要忘记使用 来检查属性hasOwnProperty

回答by Rudu

Using object iteration (key in data) and array-combining... you can return a number of elements... although the object iteration is rendered pretty useless by by the switch statement.

使用对象迭代 ( key in data) 和数组组合……您可以返回许多元素……尽管 switch 语句使对象迭代变得毫无用处。

function getElement() {
    var scope = document;
    this.by = function(data){
        var key;
        var ret=[];
        for (key in data) {
          if(data.hasOwnProperty(key)) {
            switch(key) {
              case "id":
                ret=ret.concat(scope.getElementById(data[key]));
                break;
              case "tag":
                ret=ret.concat(scope.getElementsByTagName(data[key]));
                break;
              default:
                throw new Error("Unknown property "+key);
            }
          }
        }
        return ret;
    };
}

回答by Rudu

There are lots of good generalanswers, however consider this:

有很多很好的通用答案,但是请考虑一下:

So, instead, I will cover some specific cases. First off, I generally start with:

因此,相反,我将介绍一些特定情况。首先,我通常从以下几点开始:

function f (x) {
  x = x || {} // so f() will be accepted as per f({})
  ...
}

This also sets up the context for the following.

这也为以下内容设置了上下文。

My normal approach is just to check for a truth-y value. A true value means "supplied". However this has the disadvantage of not treating 0 or '' as "supplied".

我通常的方法只是检查真值。真值意味着“提供”。然而,这具有不将 0 或 '' 视为“已提供”的缺点。

if (x.id) {
   // x.id is any truth-y
}

If 0 is an accepted input then I widen the check so that non-undefinedvalues are considered "supplied". An unset property always defaults to undefined. (This method will accept all truth-y values and false-y values such as 0, "", and null).

如果 0 是可接受的输入,那么我会扩大检查范围,以便将非undefined值视为“已提供”。未设置的属性始终默认为undefined。(此方法将接受所有真 y 值和假 y 值,例如 0、"" 和null)。

if (x.id !== undefined) {
   // x.id is all truth-y and all-but-undefined false-y
}

If undefinedis an accepted input (which I would strongly argue against), then the check can be based on hasOwnProperty. This has the dis-advantage of not checking up the [[prototype]]chain.

如果undefined是可接受的输入(我强烈反对),则检查可以基于hasOwnProperty. 这具有不检查[[prototype]]链的缺点。

if (x.hasOwnProperty("id")) {
   // x.id set to something, including undefined
}

The for(..in..)construct can also be used to iterate over the properties in an object (including properties in the [[prototype]]unless they are specially hidden). However, for the general case of dealing with input (e.g. not creating a JSON library), I find it is simple and clean just to deal with the properties on the input object(s) in one of the methods described above.

for(..in..)构造还可用于迭代对象中的属性(包括 中的属性,[[prototype]]除非它们被特别隐藏)。但是,对于处理输入的一般情况(例如,不创建 JSON 库),我发现仅用上述方法之一处理输入对象上的属性就很简单。