'in'运算符和特征检测技术...... [英] 'in' operator and feature detection technique...

查看:44
本文介绍了'in'运算符和特征检测技术......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们都知道,功能检测技术可以从支持JavaScript的用户代理非常开始。我们总是可以检查例如。

文件是否有写属性或者其他:


if(document.write){

}


我们可以为所有对象做到这一点,所以当我在''运营商'找到'b $ b'时,我感到很惊讶,以上示例将使用如下:


if(在文档中写入){


}


所以我质疑自己是为了什么?我总是这样做:


if(document [" write"]){


}


甚至

var prop = [...];


if(document [prop]){


}


支持比''in''运算符更好(''in''支持,因为

W. IE 5.5和NN 6 - 大约2000年)。我只能猜测''in''是为了方便而在W. IE 5.5中提供的

...但这是我的猜测和

更有知识的人可以写的这里有更多关于这个

运营商的意见...


BR

Luke Matuszewski

解决方案



Luke Matuszewski napisal(a):

if(document.write){
if(document [" write"]){
var prop = [...];

if(document [prop]){


,如果文档具有写属性并且它具有null
或未定值(因此在运算符中可以判断对象是否具有

中的属性,即使此属性为null或undefined)。

if(" write in quotin in document){




,如果document具有write属性,则结果为true即使它是null或

undefined。


我没有看到使用''的其他原因操作符(物体检测属性

,即使它是空的或未定义的)。这是一个测试用例:


var ob = {prop:" [财产价值]" };

if(ob.prop){

alert(" feature detection:" + ob.prop);

}


if(< prop" in ob){

alert(" in operator:" + ob.prop);

}


ob.prop = null;


if(ob.prop){

alert( 特征检测(ob.prop == null):" + ob.prop);

}


if(" prop" in ob) ){

alert(" in operator(ob.prop == null):" + ob.prop);

}


ob.prop = window.window1234567890; / * ob.prop == undefined * /


if(ob.prop){

alert(" feature detection(ob.prop == null) ):+ ob.prop;

}


if(prop在ob中){

alert (∈ in operator(ob.prop == null):" + ob.prop);

}


BR

Luke Matuszewski




Luke Matuszewski写道:

if(写在文档中){
所以我质疑自己是为了什么?我总是这样做:

if(document [" write"]){


我只能猜测'''''是
仅在方便时提供在W. IE 5.5中...




in运算符检查是否存在名称为
$ b $的属性b第一个操作数,这与将属性值

转换为布尔值(if()确实如此)大不相同。


示例


var testObject = {

b:false,

s:'''',

n:0

};


var results ='''';


for(var object property in testObject){

结果+ =''布尔值(obj ["''+ propertyName +''"]):''+

布尔值(testObject [propertyName])+''\ r \'';

结果+ =''"''+ propertyName +''"在obj:''+

(testObject中的propertyName)+''\\\\\\\ n';'

}


结果

收益


布尔值(obj [" b"]):false

" b" in obj:true


布尔值(obj [" s"]):false

" s" in obj:true


布尔值(obj [" n"]):false

" n"在obj:true

-


Martin Honnen
http://JavaScript.FAQTs.com/




Luke Matuszewski写道:

我们都知道,特征检测技术可以从支持JavaScript的用户代理开始工作。我们总是可以检查例如。
文件是否具有写属性或其他:

if(document.write){

}

我们可以为所有对象做到这一点,所以当我发现''in''运算符时我感到很惊讶,上面的示例将会像这样使用:

if(/ 写在文件中{

}

所以我质疑自己是为了什么?我总是这样做:

if(document [" write"]){

}

甚至
var prop = [...];

if(文件[prop]){

}
支持哪个''in''运算符更好(''in''支持自
W. IE 5.5和NN 6 - 大约2000年)。我只能猜测''in''在W. IE 5.5中提供仅仅是为了方便...但这是我的猜测和
更有知识的人可以在这里写一些关于此的更多意见/> operator ...




(someObject中的''property'')不适用于功能测试 - 尽管

它可以是使用它并以这种方式使用(当不需要支持史前浏览器的时候)。


但是最初它是与它一起引入的反对派hasOwnProperty for

基于原型的继承管理。


这个你可以玩的非常原始的样本是相当的

不言自明。 />

< html>

< head>

< title> Untitled Document< / title>

< meta http-equiv =" Content-Type"

content =" text / html; charset = windows-1251">

< script type =" text / javascript">


var myObject = new Object();

myObject.constructor.prototype.p1 = true;

myObject.p2 = true;


alert(''p1''在myObject中); // true

alert(myObject中的''p2''); // true


alert(myObject.hasOwnProperty(''p1'')); // false

alert(myObject.hasOwnProperty(''p2'')); // true

< / script>

< / head>


< body>


< / body>

< / html>

PS

in" operator检查对象是否具有名为property的属性。它

还会检查对象的原型,看看该属性是否是原型链的一部分。


" hasOwnProperty"如果object具有指定名称的属性

,则返回true,否则返回false。此方法不检查对象的原型链中是否存在属性

;物业必须

成为物品本身的成员。


We all know that feature detection technique works from very beggining
of user-agents supporting JavaScript. We can alway check if eg.
document has a write property or smth else:

if(document.write) {

}

We could do that for all of objects, so i have been surprised when i
found ''in'' operator, which for above example would be used like this:

if("write" in document) {

}

So i have questioned myself what for ? I can always do:

if(document["write"]) {

}

or even
var prop = [...];

if(document[prop]) {

}

which is supported better then ''in'' operator (''in'' is supported since
W. IE 5.5 and NN 6 - around 2000 year). I can only guess that ''in'' was
provided in W. IE 5.5 only for convenience... but that is my guess and
someone more knowlegable could write here some more opinions on this
operator...

B.R.
Luke Matuszewski

解决方案


Luke Matuszewski napisal(a):

if(document.write) {
if(document["write"]) {
var prop = [...];

if(document[prop]) {
, which will not work if document has a write property and it has null
or undefinded value (so in operator can tell if object has property in
question even if this property is null or undefined).
if("write" in document) {



, results true if document has write property even if it is null or
undefined.

I do not see other reasons for using ''in'' operator (detecting property
of object even if it is null or undefined). Here is a test case:

var ob = { prop: " [ property value ] " };
if(ob.prop) {
alert("feature detection: " + ob.prop);
}

if("prop" in ob) {
alert("in operator: " + ob.prop);
}

ob.prop = null;

if(ob.prop) {
alert("feature detection(ob.prop==null): " + ob.prop);
}

if("prop" in ob) {
alert("in operator(ob.prop==null): " + ob.prop);
}

ob.prop = window.window1234567890; /* ob.prop == undefined */

if(ob.prop) {
alert("feature detection(ob.prop==null): " + ob.prop);
}

if("prop" in ob) {
alert("in operator(ob.prop==null): " + ob.prop);
}

B.R.
Luke Matuszewski




Luke Matuszewski wrote:

if("write" in document) { So i have questioned myself what for ? I can always do:

if(document["write"]) {
I can only guess that ''in'' was
provided in W. IE 5.5 only for convenience...



The in operator checks whether there is a property with the name as the
first operand, that is much different to converting the property value
to a boolean (which if () does).

Example

var testObject = {
b : false,
s : '''',
n: 0
};

var results = '''';

for (var propertyName in testObject) {
results += ''Boolean(obj["'' + propertyName + ''"]): '' +
Boolean(testObject[propertyName]) + ''\r\n'';
results += ''"'' + propertyName + ''" in obj: '' +
(propertyName in testObject) + ''\r\n\r\n'';
}

results
yields

Boolean(obj["b"]): false
"b" in obj: true

Boolean(obj["s"]): false
"s" in obj: true

Boolean(obj["n"]): false
"n" in obj: true
--

Martin Honnen
http://JavaScript.FAQTs.com/



Luke Matuszewski wrote:

We all know that feature detection technique works from very beggining
of user-agents supporting JavaScript. We can alway check if eg.
document has a write property or smth else:

if(document.write) {

}

We could do that for all of objects, so i have been surprised when i
found ''in'' operator, which for above example would be used like this:

if("write" in document) {

}

So i have questioned myself what for ? I can always do:

if(document["write"]) {

}

or even
var prop = [...];

if(document[prop]) {

}

which is supported better then ''in'' operator (''in'' is supported since
W. IE 5.5 and NN 6 - around 2000 year). I can only guess that ''in'' was
provided in W. IE 5.5 only for convenience... but that is my guess and
someone more knowlegable could write here some more opinions on this
operator...



(''property'' in someObject) is not intended for features test - though
it can be used and it is used this way (when the support for
prehistoric browsers is not a requirement).

But originally it was introduced with it counterpair hasOwnProperty for
prototype-based inheritance management.

This very primitive sample you can play with is rather
self-explanatory.

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type"
content="text/html; charset=windows-1251">
<script type="text/javascript">

var myObject = new Object();
myObject.constructor.prototype.p1 = true;
myObject.p2 = true;

alert(''p1'' in myObject); // true
alert(''p2'' in myObject); // true

alert(myObject.hasOwnProperty(''p1'')); // false
alert(myObject.hasOwnProperty(''p2'')); // true
</script>
</head>

<body>

</body>
</html>
P.S.
The "in" operator checks if an object has a property named property. It
also checks the object''s prototype to see if the property is part of
the prototype chain.

The "hasOwnProperty" method returns true if object has a property of
the specified name, false if it does not. This method does not check if
the property exists in the object''s prototype chain; the property must
be a member of the object itself.


这篇关于'in'运算符和特征检测技术......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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