Javascript: get all object parameters

I have a JS object with a variable number of parameters. Is there any way to see which parameters were transferred at that particular time?

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 this

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

gets div with id chosenID

or

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

gets all inputs in a div with id chosenID ;

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

Thanks!

ps: I appreciate your time helping me, but please do not offer jQuery or other JS infrastructure, as this is for educational purposes only. Very important, Sorin.

+4
source share
3 answers

Use the for … in loop to repeat the passed parameters of the object, for example:

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

Remember to check the property with hasOwnProperty .

+5
source

Using iteration of objects ( key in data ) and union of arrays ... you can return several elements ... although iteration of the object is useless with 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; }; } 
+1
source

There are many good general answers, but think about it:

So, instead, I will talk about some specific cases. First, I usually start with:

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

It also sets the context for the following.

My usual approach is to simply check the value of true-y. True value means "delivered." However, this has the disadvantage that it does not process 0 or "as is".

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

If 0 is the accepted input, then I am expanding the check so that the values ​​are not undefined considered to be "delivered." The unset property is always undefined by default. (This method will accept all true-y 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 oppose), then the check can be based on hasOwnProperty . This has the disadvantage of not checking 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 properties of an object (including properties in [[prototype]] , unless they are specifically hidden). However, for the general case of handling input (for example, not creating a JSON library), I find it simple and clean to deal with properties on the input object (s) in one of the methods described above.

+1
source

Source: https://habr.com/ru/post/1337855/


All Articles