javascript:获取所有对象参数

发布于 2024-10-14 22:33:37 字数 788 浏览 2 评论 0原文

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

示例:

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;
    }
}

我像这样运行它,

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

获取带有 id chosenID 的 div

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

获取带有 id chosenID 的 div 中的所有inputs

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

谢谢!

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

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?

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

or

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

gets all the inputs in the div with the id 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

↘人皮目录ツ 2024-10-21 22:33:37

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

var prop;

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

不要忘记使用 hasOwnProperty

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

var prop;

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

Don't forget to check the property with hasOwnProperty.

反目相谮 2024-10-21 22:33:37

使用对象迭代(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;
    };
}

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.

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;
    };
}
一江春梦 2024-10-21 22:33:37

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

因此,我将介绍一些特定的案例。
首先,我通常从以下内容开始:

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

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

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

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

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

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

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

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

for(..in..) 构造还可用于迭代对象中的属性(包括 [[prototype]] 中的属性,除非它们是特殊的隐)。然而,对于处理输入的一般情况(例如,不创建 JSON 库),我发现只需在上述方法之一中处理输入对象的属性就非常简单和干净。

There are lots of good general answers, 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".

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

If 0 is an accepted input then I widen the check so that non-undefined values 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).

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

If undefined is 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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文