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

4.13.2 The typeof Operator

greeting += "there";


4.13.2 The typeof Operator

typeof is a unary operator that is placed before its single operand, which can be of any type. Its value is a string that specifies the type of the operand. The following table specifies the value of the typeof operator for any JavaScript value:

x typeof x
undefined "undefined"
null "object"
trueor false "boolean"
any number or NaN "number"
any string "string"
x typeof x
any function "function"
any nonfunction native object "object"
any host object An implementation-defined string, but not “undefined”, “boolean”, “number”, or “string”.

You might use the typeof operator in an expression like this: (typeof value == "string") ? "'" + value + "'" : value

The typeof operator is also useful when used with the switch statement ( §5.4.3 ). Note that you can place parentheses around the operand to typeof, which makes typeoflook like the name of a function rather than an operator keyword:

typeof(i)

Note that typeofreturns “object” if the operand value is null. If you want to distinguish null from objects, you’ll have to explicitly test for this special-case value. typeof may return a string other than “object” for host objects. In practice, however, most host objects in client-side JavaScript have a type of “object”.

Because typeofevaluates to “object” for all object and array values other than functions, it is useful only to distinguish objects from other, primitive types. In order to distinguish one class of object from another, you must use other techniques, such as the instanceof operator (see §4.9.4 ), the class attribute (see §6.8.2 ), or the constructor property (see §6.8.1 and §9.2.2 ).

Although functions in JavaScript are a kind of object, the typeof operator considers functions to be sufficiently different that they have their own return value. JavaScript makes a subtle distinction between functions and “callable objects.” All functions are callable, but it is possible to have a callable object—that can be invoked just like a function—that is not a true function. The ECMAScript 3 spec says that the typeof operator returns “function” for all native object that are callable. The ECMAScript 5 specification extends this to require that typeof return “function” for all callable objects, whether native objects or host objects. Most browser vendors use native Java-Script function objects for the methods of their host objects. Microsoft, however, has always used non-native callable objects for their client-side methods, and before IE 9 the typeof operator returns “object” for them, even though they behave like functions. In IE9 these client-side methods are now true native function objects. See §8.7.7 for more on the distinction between true functions and callable objects.

4.13 Miscellaneous Operators | 83

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