问题描述
考虑以下两个函数:
函数func1(){
var o;
if((o = document.forms)
&&(o = o [0])
&&(o = o.elements)
&&(o = o.sel)
&&(o = o.options)
&&(o = o [0] )
&&(o = o.value)
){
返回true;
}
返回false;
}
函数func2(){
if(document.forms
&& document.forms [0]
&& document.forms [0] .elements
&& document.forms [0] .elements.sel
&& document.forms [0] .elements.sel.options
&& document.forms [ 0] .elements.sel.options [0]
&& document.forms [0] .elements.sel.options [0] .value)
{
返回true;
}
返回false;
}
执行相同的功能,确保整个目标t链
在尝试引用深度之前存在宾语。这可能不是一个完美的例子,但在某些情况下,绝对有必要在对象链中一直验证
存在的事物如何期望它们
(例如,json响应)。
第一个函数似乎在IE和FF中运行速度提高了3-5倍,正如我所期望的那样>
它。
是否有更简洁的方法来编写第一个函数来获得相同的
功能但更具可读性?
-
Matt Kruse
Consider the following two functions:
function func1() {
var o ;
if ( (o=document.forms)
&& (o=o[0])
&& (o=o.elements)
&& (o=o.sel)
&& (o=o.options)
&& (o=o[0])
&& (o=o.value)
) {
return true;
}
return false;
}
function func2() {
if (document.forms
&& document.forms[0]
&& document.forms[0].elements
&& document.forms[0].elements.sel
&& document.forms[0].elements.sel.options
&& document.forms[0].elements.sel.options[0]
&& document.forms[0].elements.sel.options[0].value)
{
return true;
}
return false;
}
The perform the same function of making sure that the entire object chain
exists before trying to reference the "deep" object. This may not be a
perfect example, but in some cases it is definitely necessary to validate
all the way down the object chain that things exist how you expect them to
(for example, a json response).
The first function seems to run 3-5x faster in IE and FF, as I would expect
it to.
Is there a cleaner way to write the first function to get the same
functionality but be more readable?
--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
推荐答案
嗯,这太乱了。我不知道这是否更干净:
函数func1(){
var o = document,i = 0,ps = [' 'forms'',0,''elements'',''sel'',''options'',
0,''value''];
while(o = o [ps [i ++]]);
return(i == ps.length);
}
似乎对我有点混淆,但这是值得深思的。
-David
Hm, that''s a mess. I don''t know if this is any cleaner:
function func1() {
var o=document, i=0, ps = [''forms'', 0, ''elements'', ''sel'', ''options'',
0, ''value''];
while (o=o[ps[i++]]);
return (i == ps.length);
}
Seems a little obfuscated to me, but it''s something to ponder.
-David
嗯,这太乱了。我不知道这是否更干净:
函数func1(){
var o = document,i = 0,ps = [' 'forms'',0,''elements'',''sel'',''options'',
0,''value''];
while(o = o [ps [i ++]]);
return(i == ps.length);
}
似乎对我有点混淆,但这是值得深思的。
-David
Hm, that''s a mess. I don''t know if this is any cleaner:
function func1() {
var o=document, i=0, ps = [''forms'', 0, ''elements'', ''sel'', ''options'',
0, ''value''];
while (o=o[ps[i++]]);
return (i == ps.length);
}
Seems a little obfuscated to me, but it''s something to ponder.
-David
怎么样?
var testDeep = function(o){
o = o ||窗口;
for(var i = 1,a = arguments.length; i< a; i ++){
if(!(arguments [i] in o)){
返回undefined;
}
o = o [arguments [i]];
}
返回o;
};
testDeep(文件,''表格'',0,''元素'' ,''sel'',''options'',0,
''value'');
返回document.forms [0]。 elements.sel.options [0] .value,如果它存在。
虽然我还没有测试过,但我怀疑这个功能还是其他任何一个>
比如使用多个
赋值表达式,它的执行速度会比单曲慢。
- i
What about this?
var testDeep = function (o) {
o = o || window;
for ( var i = 1, a = arguments.length; i < a; i ++ ) {
if ( ! (arguments[i] in o) ) {
return undefined;
}
o = o[arguments[i]];
}
return o;
};
testDeep(document,''forms'', 0, ''elements'', ''sel'', ''options'', 0,
''value'');
Returns document.forms[0].elements.sel.options[0].value, if it exists.
While I haven''t tested it, I''d suspect that this function or any other
like it will perform slower than your single if with multiple
assignment expressions.
--i
< ..>
<..>
从这里看不太好。只是猜测,但是你或许打算:b $ b打算:
var testDeep = function(){
o = arguments [0] | |窗口;
....
Not so good as seen from here. Just guessing, but did you perhaps
intend:
var testDeep = function () {
o = arguments[0] || window;
....
../rh
../rh
这篇关于验证“深”的存在性宾语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!