“===”的含义对象??? [英] meaning of "===" for objects ???

查看:71
本文介绍了“===”的含义对象???的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个具有相同属性和rge相同值的对象




var o1 = {element:one,type:'' keyup'',代码:82,action:setToRed};

var o2 = {element:one,type:''keyup'',code:82,action:setToRed};


变量one是DIV元素和setToRed一个函数。


做警报(o1 === o2)给了我假。


是否意味着比较法操作而不是参考;值" ????

我需要一个合适的比较器,说eql对于包含

的对象,任何价值之王:


数字,字符串,日期,对象......


我怎么知道一个Object的类型< tag nameElement?


只能通过测试obj.nodeName?


-

UneBévue

解决方案

un ************ @ weltanschauung.com.inva lid(Une B ?? vue)写道:


假设我有两个具有相同属性和rge相同值的对象




var o1 = {element:one,type: ''keyup'',代码:82,action:setToRed};

var o2 = {element:one,type:''keyup'',code:82,action:setToRed};


变量one是DIV元素和setToRed一个函数。


做警报(o1 === o2)给了我假。


是否意味着比较法操作而不是参考;值

???



取决于对象的类型。对于* exact *含义,请参阅ecma-262

第11.9.6节。


简化(IOW不正确;经验法则):if o1和o2两个字符串

或(不是NaN)数字,o1 === o2如果它们具有相同的

值则为真。如果o1和o2属于Object类型,那么只有它们是

*相同的*对象才是真的。


我需要有一个合适的比较器,说eql对于包含

的对象,任何价值之王:


数字,字符串,日期,对象...



没有这样的比较器。 IME你实际上只需要在非常特殊的库例程中使用这样一个非常通用的比较器,例如

通用对象序列化器。你不需要在日常工作中使用它。
$ blockquote class =post_quotes>
我怎么知道一个对象是类型< ;标签nameElement?


只能通过测试obj.nodeName?



假设我理解你

,这似乎是一个相当不错的选择。这与你的其他帖子有什么关系?


-

Joost Diepenmaat |博客: http://joost.zeekat.nl/ |工作: http://zeekat.nl/

Joost Diepenmaat< jo *** @ zeekat.nlwrote:


>

没有这样的比较。 IME你实际上只需要在非常特殊的库例程中使用这样一个非常通用的比较器,例如

通用对象序列化器。你在日常生活中并不需要它。

编程。



好​​的,我现在很清楚,我必须只比较那样的对象:


var o1 = {element:one,

type:''keyup'',code:82,action:setToRed,modifiers:{sh ift:false,alt:false}};

然后使用字符串,数字简单对象具有布尔值,字符串

或数字,函数(在本例中为setToRed)和DOM元素(在此

情况下) one是DIV)。


我怎么知道Object的类型是< tag nameElement?


只能通过测试obj.nodeName?



假设我理解你

,这似乎是一个相当不错的选择。这与你的其他帖子有什么关系?



因为我需要比较具有DOM元素的对象作为

属性的值,并且我已经找到了DOM之间的比较

元素是正确的,令人惊讶(即

toto = document.getElementById(''toto'')是===到

anotherToto = document.getElementById(''toto'')。


在上面的例子中,我做比较器的方式是(简化):


- 与null双方比较;

- 如果两个对象都具有属性''nodeName''我将比较对象

身份像那样:anObject === anotherObject

- 如果不是,它是一个对象,我将递归地比较所有属性/值对

,如下:

if(!(this [p] .eql(o [p]))){return false;}


(我的比较器是原型对象:

Object.prototype.eql = function(o){...};)这个[p] .eql的原因(o [p])

替换this [p] === o [p]"

然后我需要知道说''这个对象的最佳方式是DOM类型

元素''...

-

UneBévue


Joost Diepenmaat写道:

un ** **********@weltanschauung.com.inva lid(Une B ?? vue)写道:


>让我们说我有两个具有相同属性和rge相同值的对象


var o1 = {element:one,type:''keyup'',code:82,action:setToRed};
var o2 = {element:one,type:''keyup'',code:82,action:setToRed};

变量one是DIV元素和setToRed一个功能。

警惕(o1 === o2)给了我错误。

这是否意味着比较法操作引用而不是值
? ??



取决于对象的类型。



实际上,它没有。由于对象具有同一性,如果有两个不同的对象,那么==和===的操作都将导致

false ,无论是构造函数还是原型对象。


对于* exact *含义,请参阅ecma-262第11.9.6节。



如果引用相同的对象,则两个引用相等。就这么简单。


简化(IOW不正确;经验法则):如果o1和o2都是字符串

或(不是NaN )数字,o1 === o2如果它们具有相同的价值,则为真。如果o1和o2属于Object类型,那么只有它们是

*相同*对象才是真的。



字符串和数字是_not_对象,它们是原始值。但是

字符串*对象*和数字*对象*是对象,

` ==''和'===''操作的结果是给他们的与任何对象一样:


// false

new String(" x")== new String(" x")


永远不要混淆ECMAScript实现中的原始值和对象,

特别避免使用原始值足够的对象;如果你需要对象的属性,你可以在表达式中使用它们,对于

例如:


var x = 42.3234;


// 2

x.toFixed(2).toString()。length


// 97

" a" .charCodeAt(0)

PointedEars

-

var bugRiddenCrashPronePieceOfJunk =(

navigator.userAgent.indexOf(''MSIE 5'')!= -1

&& navigator.userAgent.indexOf(''Mac' ')!= -1

)// Plone,register_function.js:16


lets say i have two objects with the same properties and rge same values
:

var o1={element:one, type:''keyup'',code:82,action:setToRed};
var o2={element:one, type:''keyup'',code:82,action:setToRed};

the variable "one" being a DIV Element and "setToRed" a function.

doing alert(o1===o2) gave me false.

does it means the comparaison operates over references not "values" ???

I need to have a proper comparator, saying "eql" for Objects containing
any king of values :

Number, string, date, object...

how could i know an Object is of type <tag nameElement ?

only by testing obj.nodeName ?

--
Une Bévue

解决方案

un************@weltanschauung.com.invalid (Une B??vue) writes:

lets say i have two objects with the same properties and rge same values
:

var o1={element:one, type:''keyup'',code:82,action:setToRed};
var o2={element:one, type:''keyup'',code:82,action:setToRed};

the variable "one" being a DIV Element and "setToRed" a function.

doing alert(o1===o2) gave me false.

does it means the comparaison operates over references not "values"
???

Depends on the type of object. For the *exact* meaning, see ecma-262
section 11.9.6.

Simplified (IOW incorrect; rule of thumb): if o1 and o2 are both strings
or (not NaN) numbers, o1 === o2 is true if they are of the same
value. If o1 and o2 are of type Object, it''s only true if they are the
*same* object.

I need to have a proper comparator, saying "eql" for Objects containing
any king of values :

Number, string, date, object...

There isn''t any such comparator. IME the you only actually need such a
very general comparator in very specific library routines such as
general object serializers. You don''t really need it in day-to-day
programming.

how could i know an Object is of type <tag nameElement ?

only by testing obj.nodeName ?

Seems like a fairly good choice, assuming I understand you
correctly. How does that relate to the rest of your post?

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/


Joost Diepenmaat <jo***@zeekat.nlwrote:

>
There isn''t any such comparator. IME the you only actually need such a
very general comparator in very specific library routines such as
general object serializers. You don''t really need it in day-to-day
programming.

Ok, that''s clear to me now, i have to compare only object like that :

var o1={element:one,
type:''keyup'',code:82,action:setToRed,modifiers:{sh ift:false,alt:false}};
then with Strings, Numbers Simple Objects having boolean values, string
or number, Function (in this case "setToRed") and DOM Element (in this
case "one" is a DIV).

how could i know an Object is of type <tag nameElement ?

only by testing obj.nodeName ?


Seems like a fairly good choice, assuming I understand you
correctly. How does that relate to the rest of your post?

because i need to compare objects having DOM Element as value of a
property and experimetally i''ve found the comparaison between to DOM
Elements is correct, surprisingly (ie
toto=document.getElementById(''toto'') is "===" to
anotherToto=document.getElementById(''toto'').

in the above ewample the way i do the "comparator" is (simplified) :

- compare with null both sides;
- if both objects have the property ''nodeName'' I''ll compare the object
identity like that : anObject===anotherObject
- if not and it is an object, I''ll compare all property/value pairs
recursively, like that :
if(!(this[p].eql(o[p]))){return false;}

(my "comparator" is a prototype of Object :
Object.prototype.eql=function(o){...};) the reason for this[p].eql(o[p])
replacing "this[p]===o[p]"
then i needed to know the best way to say ''this object is of type DOM
Element''...
--
Une Bévue


Joost Diepenmaat wrote:

un************@weltanschauung.com.invalid (Une B??vue) writes:

>lets say i have two objects with the same properties and rge same values
:

var o1={element:one, type:''keyup'',code:82,action:setToRed};
var o2={element:one, type:''keyup'',code:82,action:setToRed};

the variable "one" being a DIV Element and "setToRed" a function.

doing alert(o1===o2) gave me false.

does it means the comparaison operates over references not "values"
???


Depends on the type of object.

Actually, it does not. Since objects have identity, if there are two
different objects, both the `=='' and `==='' operations will result in
`false'', no matter their constructor or prototype object.

For the *exact* meaning, see ecma-262 section 11.9.6.

Two references are equal if they refer to the same object. As simple as that.

Simplified (IOW incorrect; rule of thumb): if o1 and o2 are both strings
or (not NaN) numbers, o1 === o2 is true if they are of the same
value. If o1 and o2 are of type Object, it''s only true if they are the
*same* object.

Strings and numbers are _not_ objects, they are primitive values. But
String *objects* and Number *objects* are objects, and the result of the
`=='' and `==='' operations is for them as with any object:

// false
new String("x") == new String("x")

Never confuse primitive values and objects in ECMAScript implementations,
especially avoid using an object where a primitive value suffices; if you
need the object''s properties, you can use them in an expression anyway, for
example:

var x = 42.3234;

// 2
x.toFixed(2).toString().length

// 97
"a".charCodeAt(0)
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf(''MSIE 5'') != -1
&& navigator.userAgent.indexOf(''Mac'') != -1
) // Plone, register_function.js:16


这篇关于“===”的含义对象???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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