what's the typeof(jQuery)
i just tri开发者_如何学运维ed this code
console.log(typeof(jQuery))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
It alerts function
, which means the typeof
jQuery is function
.
My question is, what's the exact type of jQuery? If it's function, how come it has properties like jQuery.browser
and jQuery.ajax
?
The typeof operator when applied to the jQuery
object returns the string "function"
. Basically that does mean that jQuery
is a function.
But the typing sort of stops there. Unlike statically typed languages, the number, order, modes, and types of parameters are not taken into account when computing the type of a a function. In JavaScript, it is just a "function."
When you create a function in JavaScript, the function object you create is given two properties, length
and prototype
, and its prototype is set to Function.prototype
so it has inherited properties like apply
and call
.
And as others have already answered, feel free to add your own properties. a function is just an object.
But be careful about "type." Techncially there are only SIX types in JavaScript: Null, Undefined, Boolean, Number, String, and Object. So the real answer to your question, what is the exact type of jQuery
is .... actually ... drumroll .... Object.
Edit for 2021
There are now EIGHT types in JavaScript. Symbol
and BigInt
have been added since this answer was written a decade ago.
A function is an object and can have properties in Javascript.
See https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function for a look at some of the properties a function has by default (and additional properties can be added).
Just try to do it yourself and you'll understand:
function f() {
}
f.prop = '123';
alert(f.prop);
jQuery
is a function but, of course, it is also object, that contains own functions, like call()
and can have properties as well.
精彩评论