JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript 有关的电子书:

8.7.7 Callable Objects

8.7.7 Callable Objects


We learned in §7.11 that there are “array-like” objects that are not true arrays but can be treated like arrays for most purposes. A similar situation exists for functions. A callable object is any object that can be invoked in a function invocation expression. All functions are callable, but not all callable objects are functions.

Callable objects that are not functions are encountered in two situations in today’s JavaScript implementations. First, the IE web browser (version 8 and before) implements client-side methods such as Window.alert() and Document.getElementsById() using callable host objects rather than native Function objects. These methods work the same in IE as they do in other browsers, but they are not actually Function objects. IE9 switches to using true functions, so this kind of callable object will gradually become less common.

The other common form of callable objects are RegExp objects—in many browsers, you can invoke a RegExp object directly as a shortcut for invoking its exec() method. This is a completely nonstandard feature of JavaScript that was introduced by Netscape and copied by other vendors for compatibility. Do not write code that relies on the callability of RegExp objects: this feature is likely to be deprecated and removed in the future. The typeofoperator is not interoperable for callable RegExps. In some browsers it returns “function” and in others it returns “object”.

If you want to determine whether an object is a true function object (and has function methods) you can test its class attribute ( §6.8.2 ) using the technique shown in Example 6-4 :

function isFunction(x) {

return Object.prototype.toString.call(x) === "[object Function]";

}

Note that this isFunction() function is quite similar to the isArray() function shown in §7.10 .

欢迎转载,转载请注明来自一手册:http://yishouce.com/book/1/27802.html
友情链接It题库(ittiku.com)| 版权归yishouce.com所有| 友链等可联系 admin#yishouce.com|粤ICP备16001685号-1