" x.constructor == Foo" vs" x instanceof Foo" [英] "x.constructor == Foo" vs "x instanceof Foo"

查看:67
本文介绍了" x.constructor == Foo" vs" x instanceof Foo"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的书(Flanagan的javascript :The Definitive Guide,第5版。)

暗示第111页以下内容两个结构是等价的:


(x.constructor == Foo)




(x instanceof Foo)


作者声明instanceof运算符通过检查其第一个参数'的构造函数来计算其结果

/>
财产。


然而,我最近遇到了与此相矛盾的情况。


我''在借助Firefox的Firebug调试器的帮助下,我们一直试图更好地理解jQuery。在一个断点处,有一个变量

(我称之为x)


x instanceof jQuery

是真的,但是


x.constructor == jQuery


是假的。


实际上,x.constructor是Object,但是(对象实例为jQuery)

是假的。


有人可以向我解释一下''继续?


TIA!


Kynn


-

注意:在我的地址中,第一个时期之前的所有内容都是倒退的;

和最后一个时期以及之后的所有内容都应该被丢弃。


My book (Flanagan''s javascript: The Definitive Guide, 5th ed.)
implies on page 111 that the following two constructs are equivalent:

( x.constructor == Foo )

and

( x instanceof Foo )

The author states that the instanceof operator computes its result
by examining the value of its first argument''s the constructor
property.

However, I''ve recently ran into a situation that contradicts this.

I''ve been trying to understand jQuery better, with the aid of
Firefox''s Firebug debugger. At one breakpoint, there''s one variable
(I''ll call it x) for which

x instanceof jQuery

is true, but

x.constructor == jQuery

is false.

In fact, x.constructor is Object, but (Object instanceof jQuery)
is false.

Could anyone explain to me what''s going on?

TIA!

Kynn

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.

推荐答案

1月28日上午8:47,kj< so ... @ 987jk.com.invalidwrote:
On Jan 28, 8:47 am, kj <so...@987jk.com.invalidwrote:

我的书(Flanagan的javascript :The Definitive Guide,第5版。)

暗示在第111页上,以下两个结构是等价的:


(x.constructor == Foo)





(x instanceof Foo)


作者声明instanceof运算符计算结果

通过检查其第一个参数的值''是构造函数

属性。


然而,我最近遇到了与此相反的情况。


我一直在尝试更好地理解jQuery,借助于

Firefox的Firebug调试器。在一个断点处,有一个变量

(我称之为x)


x instanceof jQuery

是真的,但是


x.constructor == jQuery


是假的。


实际上,x.constructor是Object,但是(对象实例为jQuery)

是假的。


有人可以向我解释一下''继续?


TIA!


Kynn


-

注意:在我的地址中,第一个时期之前的所有内容都是向后的;

和最后一个时期以及之后的所有内容都应该被丢弃。
My book (Flanagan''s javascript: The Definitive Guide, 5th ed.)
implies on page 111 that the following two constructs are equivalent:

( x.constructor == Foo )

and

( x instanceof Foo )

The author states that the instanceof operator computes its result
by examining the value of its first argument''s the constructor
property.

However, I''ve recently ran into a situation that contradicts this.

I''ve been trying to understand jQuery better, with the aid of
Firefox''s Firebug debugger. At one breakpoint, there''s one variable
(I''ll call it x) for which

x instanceof jQuery

is true, but

x.constructor == jQuery

is false.

In fact, x.constructor is Object, but (Object instanceof jQuery)
is false.

Could anyone explain to me what''s going on?

TIA!

Kynn

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.



我知道instanceof与x.constructor确定的方式有区别。在这种情况下,它听起来像是JQuery处理它的扩展功能的方式的问题。不是很好的使用JQuery,你能否说明JQuery扩展的方式是什么?

I know that there are difference in the way that instanceof is
determined vs. x.constructor. In this instance it sounds like it''s an
issue with the way that JQuery handles it''s extends functionality. Not
having used JQuery a great deal, could you shed some light on the way
that JQuery extends?


在< 7a ********************************** @ e10g2000prf。 googlegroups.com"李******* @ gmail.com" < li ******* @ gmail.comwrites:
In <7a**********************************@e10g2000prf. googlegroups.com"li*******@gmail.com" <li*******@gmail.comwrites:

>我知道确定instanceof的方式有所不同与x.constructor。在这种情况下,它听起来像是JQuery处理它的扩展功能的方式的问题。不是很好地使用了JQuery,你能否说明JQuery扩展的方式?
>I know that there are difference in the way that instanceof is
determined vs. x.constructor. In this instance it sounds like it''s an
issue with the way that JQuery handles it''s extends functionality. Not
having used JQuery a great deal, could you shed some light on the way
that JQuery extends?



这让我头疼! :-)但我会尽我所能...


jQuery对象确实有一个extend方法,如果这就是你的内容b $ b问。它可以通过几种方式调用,但可能最常用的是
一般类似于


jQuery.extend(bool,target,obj1,obj2, obj3 ...);


如果bool为false,则将obj1,obj2,obj3等的值复制到目标的相应插槽中。

。如果bool为true,那么当b / b
目标和源槽的内容本身都是对象时,复制它会扩展插槽。 (那是一个非常好的

广泛描述!)


但我发现你的问题也令人费解,因为它表明

" extends"是一个函数,在可用时,在某些情况下由解释器自动调用,例如,valueOf

或toString。如果是这种情况,那么我之前比之前更加神秘,因为我从来没有听说过这样的事情。 (FWIW,Flanagan

甚至不提*延伸。)


Kynn


P.S.我真的需要给自己一个权威的参考

JavaScript ...我认为Flanagan的书就是它,但是今天我只是碰到了
一些主题(例如const关键字)

Flanagan甚至不提*。我想知道是否延长将

成为另一个这样的盲点...


-

注意:在我的地址中,第一期之前的所有内容都是向后;

和最后一段时间以及它之后的所有内容都应该被丢弃。

That went over my head! :-) But I''ll do my best...

The jQuery object does have an extend method, if that''s what you''re
asking. It can be invoked in a few ways, but probably the most
general one is something like

jQuery.extend( bool, target, obj1, obj2, obj3 ... );

If bool is false, extend copies values from obj1, obj2, obj3, etc.
into the corresponding slots in target. If bool is true, instead
of copying it extends the slots when the the contents of both the
target and source slots are themselves are objects. (That''s a very
broad-strokes description!)

But I find your question also puzzling, because it suggests that
"extends" is a function that, when available, gets called automatically
by the interpreter in certain situations, just like, e.g., valueOf
or toString. If this is the case then I''m even more mystified than
before, because I''ve never heard of such a thing. (FWIW, Flanagan
doesn''t even *mention* extend.)

Kynn

P.S. I really need to get myself an authoritative reference on
JavaScript... I thought that Flanagan''s book was it, but just
today I''ve run into a few topics (such as the const keyword) that
Flanagan doesn''t even *mention*. I''m wondering if "extend" will
be another such blind spot...

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.


Joost Diepenmaat< jo *** @ zeekat。 nlwrites:
Joost Diepenmaat <jo***@zeekat.nlwrites:

什么?只是。不,那甚至更令人困惑。如果你想要真正的原型设计,为什么不这样做呢?


var SubProto = {};

var SuperProto = extend(SubProto) );

var super = extend(sub);
What? Just. No. That''s just even *more* confusing. Why not do this if
you want real prototyping:

var SubProto = {};
var SuperProto = extend(SubProto);
var super = extend(sub);



我的意思是:


var SubProto = {};

var SuperProto = extend( SubProto);

var super = extend(SuperProto);


对于混乱感到抱歉。


Joost。

I meant:

var SubProto = {};
var SuperProto = extend(SubProto);
var super = extend(SuperProto);

Sorry about the confusion.

Joost.


这篇关于&quot; x.constructor == Foo&quot; vs&quot; x instanceof Foo&quot;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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