什么时候功能不是功能? [英] When is a function not a function?

查看:56
本文介绍了什么时候功能不是功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列接受函数或对象的函数

(与方法名称配对)作为回调。


For我依靠这个测试的最长时间。


(typeof cb ==''function'')


这也应该有效,但我不确定它在旧版浏览器中的降价情况有多好。我认为在

另一个上下文(例如框架)中创建的函数也存在问题。


(cb instanceof函数)

>
然后我遇到了这个。


(!! fn&& typeof fn!=" string"&&!fn.nodeName&& ; fn.constructor!=

数组&& /function/i.test(fn +""))


我是确定它是错误的,因为它是来自jQuery,但它让我想知道是否在旧版浏览器中存在错误导致我的测试错误。


作为除此之外,我正在将jQuery源视为一些浏览器

嗅探器我在博客上引用它作为每个人

正在做的事情的一部分。论点。我先前已经瞥了一眼,并根据它与Prototype的相似性而驳回了它的价值。仔细观察后,它会让原型看起来更具启发性。可以说任何人都会建议它没有阅读代码。这是一个恐怖表演。

I''ve got a collection of functions that accept a function or object
(paired with a method name) as a callback.

For the longest time I have relied on this test.

(typeof cb == ''function'')

This should work as well, but I am not sure how well it degrades in
older browsers. I think there are issues with functions created in
another context (eg frame) as well.

(cb instanceof Function)

Then I came across this.

(!!fn && typeof fn != "string" && !fn.nodeName && fn.constructor !=
Array && /function/i.test( fn + "" ))

I''m sure it is wrong as it is from jQuery, but it makes me wonder if
there are bugs in older browsers that will mislead my test.

As an aside, I was looking at the jQuery source as some browser
sniffer I was arguing with on a blog cited it as part of an "everybody
is doing it" argument. I had glanced at it previously and dismissed
it based on its resemblance to Prototype. Upon closer inspection, it
makes Prototype look inspired. Suffice to say that anybody who
recommends it hasn''t read the code. It''s a horror show.

推荐答案

9月25日晚上7:09,David Mark< dmark.cins .. 。@ gmail.comwrote:
On Sep 25, 7:09 pm, David Mark <dmark.cins...@gmail.comwrote:

我有一系列接受函数或对象的函数

(与方法配对)姓名)作为回调。


最长的时间我依靠这个测试。


(typeof cb ==''function' ')


这应该也可以,但我不确定它在旧版浏览器中的降级程度如何。我认为在

另一个上下文(例如框架)中创建的函数也存在问题。


(cb instanceof函数)

>
然后我遇到了这个。


(!! fn&& typeof fn!=" string"&&!fn.nodeName&& ; fn.constructor!=

数组&& /function/i.test(fn +""))


我是确定它是错误的,因为它来自jQuery,但它让我想知道是否在旧浏览器中存在误导我的测试的错误。
I''ve got a collection of functions that accept a function or object
(paired with a method name) as a callback.

For the longest time I have relied on this test.

(typeof cb == ''function'')

This should work as well, but I am not sure how well it degrades in
older browsers. I think there are issues with functions created in
another context (eg frame) as well.

(cb instanceof Function)

Then I came across this.

(!!fn && typeof fn != "string" && !fn.nodeName && fn.constructor !=
Array && /function/i.test( fn + "" ))

I''m sure it is wrong as it is from jQuery, but it makes me wonder if
there are bugs in older browsers that will mislead my test.



我不想假设不正确,因为它在jQuery中。如果他们在旧浏览器中测试,我会感到惊讶。时髦的图书馆

似乎不这样做。

I don''t like to assume incorrect because it is in jQuery. I am
surprised if they are testing in older browsers. The hip libraries
don''t seem to do that.


顺便说一下,我在看jQuery作为一些浏览器来源

嗅探器我在博客上争论说它是每个人都是这样做的一部分

正在做的事情论点。我先前已经瞥了一眼,并根据它与Prototype的相似性而驳回了它的价值。仔细观察后,它会让原型看起来更具启发性。可以说任何人都会建议它没有阅读代码。这是一场恐怖秀。
As an aside, I was looking at the jQuery source as some browser
sniffer I was arguing with on a blog cited it as part of an "everybody
is doing it" argument. I had glanced at it previously and dismissed
it based on its resemblance to Prototype. Upon closer inspection, it
makes Prototype look inspired. Suffice to say that anybody who
recommends it hasn''t read the code. It''s a horror show.



在谷歌的一次谈话中,jQuery创始人John Resig在与浏览器漏洞斗争足够长时间之后说了一些像



JavaScript程序只使用navigator.userAgent。至少那是我怎么理解他的谈话。他确实说过许多非常好的事情。您可以观看视频。

http://ejohn.org/blog/building-a-javascript-library/

我在该博客页面上留下了评论嗅闻他从来没有做过

真的回应。也许他后来在评论中提到的博客文章仍然在进行中。我很想读它。


棘手的一个:你如何提供测试不透明度支持?


彼得

In a talk at Google, John Resig, jQuery founder, said something like
"after fighting with browser bugs long enough an experienced
JavaScript program just uses navigator.userAgent." At least that is
how I understood his talk. He does say many really good things. You
can watch the video.

http://ejohn.org/blog/building-a-javascript-library/

I left a comment on that blog page about sniffing and he never did
really respond. Perhaps that future blog post to which he refers later
in the comments is still in the works. I''m interested to read it.

Tricky one: How do you feature test opacity support?

Peter


9月26日下午1:02,David Mark< dmark.cins ... @ gmail.comwrote:
On Sep 26, 1:02 pm, David Mark <dmark.cins...@gmail.comwrote:

jQuery使用美元符号变量,我认为不允许在规范下使用

jQuery uses dollar sign variables, which I don''t think are allowed
under the specification.



允许美元符号。见第7.6节。该规范然后说美元

标志仅用于机械生成的代码。


阅读所有关于美元符号的骚动变量甚至是
一个字符的美元符号变量,我认为如果他们将此意图声明排除在规范之外,那将会更好

。一美元

标志可以是也可以不是标识符。如果可以,那么由

程序员来决定如何使用它。如果我在过去的100年里一直在编写JavaScript以获得
并且知道JavaScript会向后兼容

新关键字除外,我可能会开始我所有的

带有美元符号的标识符(例如

Dollar signs are allowed. See sec 7.6. The spec then says "The dollar
sign is intended for use only in mechanically generated code."

After reading all the uproar about dollar signs in variables or even
one character dollar sign variable, I think it would have been better
if they had left this statement of intention out of the spec. A dollar
sign can or cannot be in an identifier. If it can then it is up to the
programmer to decide how to use it. If I was writing JavaScript to
last 100 years and knew JavaScript would be backwards compatible with
the exception of new keywords, I would probably start all my
identifiers with a dollar sign (eg.


foo,


这篇关于什么时候功能不是功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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