jQuery的isFunction和InternetExplorer [英] jQuery's isFunction and InternetExplorer

查看:86
本文介绍了jQuery的isFunction和InternetExplorer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当适应jQuery的isFunction方法时

When adapting jQuery's isFunction method

isFunction: function( obj ) {
  return toString.call(obj) === "[object Function]";
}

InternetExlorer 8返回以下错误:对象不支持此属性或方法。 一篇好文章深入研究这个函数并不能解决这个问题。为了检查是否已经定义了 obj ,我更改了参考中的函数到MSDN文章

InternetExlorer 8 returns the following error: "Object doesn't support this property or method". A good article digging into this function won't fix this behaviour. To check wether obj is defined, I changed the function in reference to the MSDN article:

isFunction: function( obj ) {
  return obj && toString.call(obj) === "[object Function]";
}

解决方案的其他想法是什么?

Any other ideas for a solution?

推荐答案

你需要直接从 Object.prototype中调用 toString 方法 object:

You need to call the toString method directly from the Object.prototype object:

function isFunction (obj) {
    return Object.prototype.toString.call(obj) === "[object Function]";
}

alert(isFunction({})); // false
alert(isFunction(function{})); // true

jQuery有一个名为 toString的本地变量指的是 Object.prototype 上的方法。

jQuery has a local variable named toString that refers to the method on Object.prototype.

只调用 toString。调用(obj); 而不在范围上声明 toString 标识符,适用于Firefox,Chrome等,因为Global对象继承自 Object.prototype ,但规范并不能保证这一点。

Calling just toString.call(obj); without declaring a toString identifier on scope, works on Firefox, Chrome, etc, just because the Global object inherits from Object.prototype, but this is not guaranteed by the spec.

Object.prototype.isPrototypeOf(window); // false on IE

您链接的文章谈到了ECMAScript 5th中引入的更改版本规范为调用适用方法。

The article you link to talks about a change that was introduced in the ECMAScript 5th Edition specification to the call and apply methods.

这些方法允许您调用传递第一个参数的函数作为调用函数的 this 值。

Those methods allow you to invoke a function passing the first argument as the this value of the invoked function.

On ECMAScript 3,如果此参数为 undefined null ,则被调用函数的值将引用全局对象,例如:

On ECMAScript 3, if this argument was undefined or null, the this value of the invoked function will refer to the global object, for example:

function test () {  return this; }
test.call(null) === window; // true

但是在ES5中改变了,现在应该传递该值而不进行修改,这导致了 Object.prototype.toString 抛出异常,因为期望有一个对象参数。

But that changed in ES5, the value should now be passed without modification and that caused the Object.prototype.toString to throw an exception, because an object argument was expected.

该方法的规范更改,现在如果值引用 undefined null 将返回字符串[object Undefined][object Null],修复问题(我不认为的事情真的很好,因为两个结果都感觉错了, undefined null not 对象,它们是原始的...让我记住 typeof null =='object' ...而且我觉得它与 [[Class]] 内部属性的概念,无论如何......)

The specification of that method changed, now if the this value refers to undefined or null the string "[object Undefined]" or "[object Null]" will be returned, fixing the problem (thing that I don't think is really good, since both results results feel just wrong, undefined and null are not objects, they are primitives... that made me remember typeof null == 'object'... Moreover I think it messes up with the concept of the [[Class]] internal property, anyway...)

现在你可能想知道为什么jQuery使用此方法检查函数对象,而不是使用 typeof 运算符?

Now you might wonder why jQuery uses this method to check for a function object, instead of using the typeof operator?

执行错误,例如在Chrome / Safari / WebKit中,RegExp对象的 typeof 返回function,因为RegExp对象的地址为 callable ,例如:

Implementation bugs, for example in Chrome/Safari/WebKit, the typeof for RegExp objects returns "function", because RegExp objects where made callable, e.g.:

typeof /foo/; // "function" in Chrome/Safari/WebKit

typeof 运算符返回function如果对象实现 [[Call]] internal属性,使对象可调用

The typeof operator returns "function" if the object implements the [[Call]] internal property, which made objects to be callable.

这最初是由Mozilla实现引入的,调用RegExp对象相当于调用 exec 方法。

This was originally introduced by the Mozilla implementations, invoking a RegExp object is equivalent to call the exec method.

这篇关于jQuery的isFunction和InternetExplorer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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