javascript:获取所有对象参数
我有一个带有可变数量参数的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
for … in
循环迭代传递的对象的参数,例如:不要忘记使用
hasOwnProperty
。Use a
for … in
loop to iterate over the passed object's parameters, like:Don't forget to check the property with
hasOwnProperty
.使用对象迭代(
key in data
)和数组组合...您可以返回许多元素...尽管 switch 语句使对象迭代变得毫无用处。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.有很多很好的通用答案,但是请考虑一下:
因此,我将介绍一些特定的案例。
首先,我通常从以下内容开始:
这也为以下内容设置了上下文。
我的正常方法只是检查真实值。真值意味着“已提供”。然而,这有一个缺点,即不将 0 或 '' 视为“已提供”。
如果 0 是可接受的输入,那么我会扩大检查范围,以便将非
未定义
值视为“已提供”。未设置的属性始终默认为未定义
。 (此方法将接受所有 true-y 值和 false-y 值,例如 0、“”和null
)。如果
undefined
是接受的输入(我强烈反对),那么检查可以基于hasOwnProperty
。这样做的缺点是不检查[[prototype]]
链。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:
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 0 is an accepted input then I widen the check so that non-
undefined
values are considered "supplied". An unset property always defaults toundefined
. (This method will accept all truth-y values and false-y values such as 0, "", andnull
).If
undefined
is an accepted input (which I would strongly argue against), then the check can be based onhasOwnProperty
. This has the dis-advantage of not checking up the[[prototype]]
chain.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.