什么是类型(jQuery)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6922256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what's the typeof(jQuery)
提问by Rusi Nova
i just tried 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
.
它发出警报function
,这意味着typeof
jQuery 是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
?
我的问题是,jQuery 的确切类型是什么?如果它是函数,它为什么具有jQuery.browser
和这样的属性jQuery.ajax
?
采纳答案by Ray Toal
The typeof operator when applied to the jQuery
object returns the string "function"
. Basically that does mean that jQuery
is a function.
typeof 运算符应用于 jQuery
对象时返回字符串"function"
。基本上这确实意味着这jQuery
是一个函数。
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."
但是打字就到此为止了。与静态类型语言不同,计算 aa 函数的类型时不考虑参数的数量、顺序、模式和类型。在 JavaScript 中,它只是一个“函数”。
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
.
当你在 JavaScript 中创建一个函数时,你创建的函数对象被赋予两个属性length
和prototype
,并且它的原型被设置为Function.prototype
所以它继承了像apply
和这样的属性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 SIXtypes 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.
但要小心“类型”。Techncially只有SIX在JavaScript类型:null,未定义,布尔型,数字,字符串和对象。所以你的问题的真正答案,什么jQuery
是......的确切类型是 .... 实际上......鼓声 .... Object。
回答by jfriend00
A function is an object and can have properties in Javascript.
函数是一个对象,可以在 Javascript 中具有属性。
See https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Functionfor a look at some of the properties a function has by default (and additional properties can be added).
请参阅https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function以查看函数默认具有的一些属性(并且可以添加其他属性)。