开发者

javascript: get all object parameters

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 a开发者_JAVA技巧ll 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.


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜