什么是类型(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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:34:00  来源:igfitidea点击:

what's the typeof(jQuery)

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 typeofjQuery is function.

它发出警报function,这意味着typeofjQuery 是function.

My question is, what's the exact type of jQuery? If it's function, how come it has properties like jQuery.browserand jQuery.ajax?

我的问题是,jQuery 的确切类型是什么?如果它是函数,它为什么具有jQuery.browser和这样的属性jQuery.ajax

采纳答案by Ray Toal

The typeof operator when applied to the jQueryobject returns the string "function". Basically that does mean that jQueryis 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, lengthand prototype, and its prototype is set to Function.prototypeso it has inherited properties like applyand call.

当你在 JavaScript 中创建一个函数时,你创建的函数对象被赋予两个属性lengthprototype,并且它的原型被设置为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 jQueryis .... 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以查看函数默认具有的一些属性(并且可以添加其他属性)。

回答by Sergey Metlov

Just try to do it yourself and you'll understand:

只要尝试自己做,你就会明白:

function f() {
}

f.prop = '123';

alert(f.prop);

jQueryis a functionbut, of course, it is also object, that contains own functions, like call()and can have properties as well.

jQuery是一个函数,当然,它也是一个对象,它包含自己的函数,比如call()并且可以有属性。