isFunction(代码值得推荐的项目) [英] isFunction (Code Worth Recommending Project)

查看:68
本文介绍了isFunction(代码值得推荐的项目)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到何时功能不是功能?线程再次爆发了




这是我用来测试函数或

对象的参数。它确实返回了可调用主机对象的预期结果

(至少是我测试过的那些。)它支持的函数都不是

期望接收它们作为参数。测试的第二部分

在这方面有点含糊不清(对于不支持

调用的浏览器),所以最好将可调用主机对象排除为规则。


var isFunction = function(o){

return typeof(o)==''function''&& (!Function.prototype.call ||

typeof(o.call)==''function'');

};

I see the "When is a function not a function" thread has flared up
again.

This is what I use to test parameters that can be Functions or
Objects. It does return the expected result for callable host objects
(at least the ones I tested.) None of the functions it supports are
expected to receive those as parameters. The second part of the test
is a little ambiguous in this regard (for browsers that do not support
call), so it is best to exclude callable host objects as a rule.

var isFunction = function(o) {
return typeof(o) == ''function'' && (!Function.prototype.call ||
typeof(o.call) == ''function'');
};

推荐答案

David Mark写道:
David Mark wrote:

我看到何时函数不是函数线程再次爆发了




这是我用来测试函数或

对象的参数。它确实返回了可调用主机对象的预期结果

(至少是我测试过的那些。)它支持的函数都不是

期望接收它们作为参数。测试的第二部分

在这方面有点含糊不清(对于不支持

调用的浏览器),所以最好将可调用主机对象排除为规则。


var isFunction = function(o){

return typeof(o)==''function''&& (!Function.prototype.call ||

typeof(o.call)==''function'');

};
I see the "When is a function not a function" thread has flared up
again.

This is what I use to test parameters that can be Functions or
Objects. It does return the expected result for callable host objects
(at least the ones I tested.) None of the functions it supports are
expected to receive those as parameters. The second part of the test
is a little ambiguous in this regard (for browsers that do not support
call), so it is best to exclude callable host objects as a rule.

var isFunction = function(o) {
return typeof(o) == ''function'' && (!Function.prototype.call ||
typeof(o.call) == ''function'');
};



isFunction()将返回`false'',其中Function.prototype.call不支持
且对象没有'call' '属性,即使参数

引用了一个Function对象。在Netscape Navigator版本4.05及更早版本支持
版本1.3之前的JavaScript中就是这种情况,

,更重要的是,版本5.5之前的JScript支持例如通过

Microsoft Internet Explorer for Windows NT,版本5.01及更早版本。


参数可能是一个不合格的参考,在这种情况下调用此
$如果之前未定义该引用的标识符,b $ b测试方法将失败。


分配函数创建的函数对象没有意义

在变量的初始化中表达而不是简单的函数

声明,除非该代码是条件执行块的一部分。


`typeof''是一个操作员,而不是一个方法,应该相应地写。

PointedEars

-

Prototype.js是由那些不喜欢的人写的不懂人的javascript

谁不懂javascript。不知道javascript的人不是设计使用javascript的系统的最佳建议来源。

- Richard Cornford,cljs,< f8 ** *****************@news.demon.co.uk>

isFunction() will return `false'' where Function.prototype.call is not
supported and the object has no `call'' property, even though the argument
referred to a Function object. That is the case in JavaScript before
version 1.3 as supported by Netscape Navigator, version 4.05 and earlier,
and, more important, JScript before version 5.5 as supported e.g. by
Microsoft Internet Explorer for Windows NT, version 5.01 and earlier.

The argument may be an unqualified reference in which case calling this
testing method fails if the identifier of that reference was not defined before.

There is no point in assigning a function object created by a function
expression in the initialization of a variable instead of a simple function
declaration, unless that code is part of a conditional execution block.

`typeof'' is an operator, not a method, and should be written accordingly.
PointedEars
--
Prototype.js was written by people who don''t know javascript for people
who don''t know javascript. People who don''t know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>


12月8日上午11:05, David Mark< dmark.cins ... @ gmail.comwrote:
On Dec 8, 11:05 am, David Mark <dmark.cins...@gmail.comwrote:

12月8日下午12:22,Thomas''PointedEars''Lahn< PointedE ... @ web.de>

写道:
On Dec 8, 12:22 pm, Thomas ''PointedEars'' Lahn <PointedE...@web.de>
wrote:

David Mark写道:
David Mark wrote:

12月8日上午11点07分,Thomas''PointedEars''Lahn< PointedE ... @ web.dewrote:

> David Mark写道:

>> 12月8日上午9:33,Thomas''PointedEars''Lahn< PointedE ... @ web.de>

>>写道:

>>> David Mark写道:

>>>>我看到何时功能不是功能?线程再次爆发

>>>>再次。这就是我用来测试参数的方法,这些参数可以是

>>>>函数或对象。它确实返回了

>>>>可调用主机对象(至少是我测试过的那些)的预期结果。没有

>> >>它支持的功能预计将接收那些作为

>>>>参数。测试的第二部分有点含糊不清

>>>>这方面(适用于不支持通话的浏览器),所以它是

>>>>最好将可调用主机对象排除在规则之外。 var isFunction =

>>>> function(o){return typeof(o)==''function''&&

> >>>(!Function.prototype.call || typeof(o.call)==''function''); };

>>> isFunction()将返回`false'',其中Function.prototype.call是

>>>不支持object没有`call''属性,即使

>>>引用了Function对象的参数。这是

>>>之前的JavaScript

>>如果它是一个函数,则结合的第一部分为真。

>>第二部分也是如此!当不支持呼叫时,Function.prototype.call将

>>评估为true。那么你在说什么?b $ b>>谈论什么?

>对不起,我在这里很困惑。但是,我想指出

>表示没有必要对

> Function.prototype.call进行测试,无论是在负面直接还是积极的

>间接方式。该对象具有(n *外部*)调用属性具有

>与是否可调用无关(带有参数

> ; list),具有* internal * [[Call]]方法。
On Dec 8, 11:07 am, Thomas ''PointedEars'' Lahn <PointedE...@web.dewrote:
>David Mark wrote:
>>On Dec 8, 9:33 am, Thomas ''PointedEars'' Lahn <PointedE...@web.de>
>>wrote:
>>>David Mark wrote:
>>>>I see the "When is a function not a function" thread has flared
>>>>up again. This is what I use to test parameters that can be
>>>>Functions or Objects. It does return the expected result for
>>>>callable host objects (at least the ones I tested.) None of the
>>>>functions it supports are expected to receive those as
>>>>parameters. The second part of the test is a little ambiguous in
>>>>this regard (for browsers that do not support call), so it is
>>>>best to exclude callable host objects as a rule. var isFunction =
>>>>function(o) { return typeof(o) == ''function'' &&
>>>>(!Function.prototype.call || typeof(o.call) == ''function''); };
>>>isFunction() will return `false'' where Function.prototype.call is
>>>not supported and the object has no `call'' property, even though
>>>the argument referred to a Function object. That is the case in
>>>JavaScript before
>>If it is a Function then the first part of the conjunction is true.
>>The second part will also be true as !Function.prototype.call will
>>evaluate to true when call is not supported. So just what are you
>>talking about?
>Sorry, I have confused matters here. However, I would like to point
>out that it does not make sense to test against
>Function.prototype.call, be it in the negative direct or the positive
>indirect way. That the object has a(n *external*) `call'' property has
>nothing to do with whether or not it is callable (with an argument
>list), having an *internal* [[Call]] method.


除了函数之外,它并不意味着返回true。它不是一个

isCallable函数。
It is not meant to return true for anything but Functions. It is not an
isCallable function.


再次,如果我已经知道参数是对函数

对象的引用,我不需要(打电话)你的方法。因为Function对象
Again, if I already knew that the argument is a reference to a Function
object, I would not need (to call) your method. Because Function objects



你显然没有注意到。让我试着为你简化这个
。这个函数对我来说可以这么简单:


typeof(o)==''function''


调用函数做不知道他们的论点是否是

函数或其他类型的对象。当他们通过这个测试被确定为
be功能时,他们被称为。如果没有,则调用

对象的方法。它是专门为回调创建的,因为我已经提到了
。如何将其翻译成我已经知道

参数是对Function对象的引用在此之后

解释了三次已经超出了我。


如果你回去看原帖,你会理解为什么

添加了额外的逻辑(清除可调用的主机对象和

方法。)


You apparently haven''t been paying attention. Let me try to simplify
this for you. This function for my purposes could be as simple as:

typeof(o) == ''function''

The calling functions do NOT know whether their argument(s) are
Functions or some other type of object. When they ARE DETERMINED TO
BE FUNCTIONS by this test, they are called. When not, a method of the
object is called. It was created specifically for callbacks as I
already mentioned. How you translate that into "I already knew the
argument is a reference to a Function object" after this has been
explained three times already is beyond me.

If you go back and read the original thread, you will understand why
the additional logic was added (to weed out callable host objects and
methods.)



我知道一个可调用的宿主对象可能不是功能但如果我是使用名为isFunction的函数的
我希望如果我发送任何

可调用对象,它将返回true。

I know a callable host object may not be a "function" but if I was
using a function called "isFunction" I''d expect that if I send it any
callable object it would return true.


它更像是一个学术练习而不是任何事情和

考虑之后,我认为它不需要在存储库中。
It is more of an academic exercise than anything and after
thinking about it, I don''t think it needs to be in the repository.



我同意(至少目前为止)。我认为存储库应该获得这样的基础函数,如更大的代码片段所需要的那样(例如Ajax,事件等)。我从来没有创建过重的JavaScript

函数,其中重载参数可以是函数。至于

,据我所知,我只有一个

元素或元素id字符串的重载参数。因此,我并没有真正地跟随各种各样的当一个功能不是一个功能时,

线程。


[snip]


-

彼得

代码值得推荐的项目
http://cljs.michaux.ca


12月8日晚上10点32分,RobG< rg ... @ iinet.net.auwrote:
On Dec 8, 10:32 pm, RobG <rg...@iinet.net.auwrote:

Peter Michaux写道:
Peter Michaux wrote:

12月8日上午11:05,David Mark< dmark.cins ... @ gmail.comwrote:
On Dec 8, 11:05 am, David Mark <dmark.cins...@gmail.comwrote:

> ;

这是一次学术活动而不是任何事情,在考虑它之后,我不认为它需要在

存储库。
>
It is more of an academic exercise than anything and after
thinking about it, I don''t think it needs to be in the
repository.


我同意(至少目前为止)。我认为存储库应该获得

这样的基础函数,因为需要更大的
代码(例如Ajax,事件等)。
I agree (at least for now). I think the repository should gain
foundational functions like this as needed for bigger pieces of
code (e.g. Ajax, events, etc).



似乎一致同意确定某个对象是否是一个

函数并不能确定它是否可以被调用,并且

一般需要确定是否可以调用某些东西。因此,需要一种isCallable函数。


There seems to be agreement that determining whether an object is a
function is not a reliable indicator of whether it may be called, and
that there is a general need to determine whether or not something is
callable. Therefore, there is a need for an isCallable function.



看起来这样的功能至少很难编写,并且没有它就会让人失望。

当然可以编写浏览器脚本库的所有基本部分,而不需要这样的功能。如果对存储库中具有这样的函数

感兴趣,并且可以编写这样的函数来可靠地执行

,那么它应该被添加到存储库中。 br />

-

彼得

代码值得推荐的项目
http://cljs.michaux.ca


这篇关于isFunction(代码值得推荐的项目)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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