验证“深”的存在性宾语 [英] Validating existence of "deep" object

查看:72
本文介绍了验证“深”的存在性宾语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下两个函数:


函数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
http://www.JavascriptToolbox.com
http:// www .AjaxToolbox.com

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

推荐答案

2月16日上午6:35,Matt Kruse < newsgro ... @ mattkruse.comwrote:
On Feb 16, 6:35 am, "Matt Kruse" <newsgro...@mattkruse.comwrote:

考虑以下两个函数:


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)

){

返回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;


}


执行相同的功能以确保整个对象链

在尝试引用深度之前存在宾语。这可能不是一个完美的例子,但在某些情况下,绝对有必要在对象链中一直验证

存在的事物如何期望它们

(例如,json响应)。


第一个函数似乎在IE和FF中运行速度提高了3-5倍,正如我所期望的那样>
它。


是否有更简洁的方法来编写第一个函数来获得相同的

功能但更具可读性?


-

Matt Krusehttp://www.JavascriptToolbox.comhttp://www.AjaxToolbox.com
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 Krusehttp://www.JavascriptToolbox.comhttp://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


2月16日下午12:11,David Golightly < davig ... @ gmail.comwrote:
On Feb 16, 12:11 pm, "David Golightly" <davig...@gmail.comwrote:

2月16日上午6:35,Matt Kruse < newsgro ... @ mattkruse.comwrote:
On Feb 16, 6:35 am, "Matt Kruse" <newsgro...@mattkruse.comwrote:

考虑以下两个函数:
Consider the following two functions:


函数func1(){

var o;

if((o = document.forms)

&&(o = o [0])

&&(o = o.elements)

&&(o = o.sel)

&&(o = o.options)

&&(o = o [0])

&&(o = o。价值)

){

返回true;

}

返回false;
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)

{

返回true;

}

返回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;


}
}


执行相同的功能以确保整个对象链

在尝试引用深度之前存在。宾语。这可能不是一个完美的例子,但在某些情况下,绝对有必要在对象链中一直验证

存在的事物如何期望它们

(例如,json响应)。
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).


第一个函数似乎在IE和FF中运行速度提高了3-5倍,正如我所期望的那样...... / b $ b。
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 Krusehttp://www.JavascriptToolbox.comhttp://www.AjaxToolbox.com
--
Matt Krusehttp://www.JavascriptToolbox.comhttp://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



怎么样?


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


2月16日下午4:26,Isaac Schlueter < isaacschlue ... @ gmail.com>

写道:
On Feb 16, 4:26 pm, "Isaac Schlueter" <isaacschlue...@gmail.com>
wrote:

2月16日下午12:11,David Golightly ; < davig ... @ gmail.comwrote:
On Feb 16, 12:11 pm, "David Golightly" <davig...@gmail.comwrote:

2月16日上午6:35,Matt Kruse < newsgro ... @ mattkruse.comwrote:
On Feb 16, 6:35 am, "Matt Kruse" <newsgro...@mattkruse.comwrote:


考虑以下两个函数:
Consider the following two functions:



< ..>

<..>


>

怎么样?


var testDeep = function(o){

o = o ||窗口;
>
What about this?

var testDeep = function (o) {
o = o || window;



从这里看不太好。只是猜测,但是你或许打算: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;

....


testDeep(document,''forms'',0,''elements' ',''sel'',''options'',0,''value'');
testDeep(document,''forms'', 0, ''elements'', ''sel'', ''options'', 0, ''value'');



../rh


../rh


这篇关于验证“深”的存在性宾语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆