What type (jQuery)

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 notifies function , which means typeof jQuery function .

My question is: which type of jQuery is it? If it works, then how does it have properties like jQuery.browser and jQuery.ajax ?

+6
source share
3 answers

The typeof operator applied to a jQuery object returns the string "function" . This basically means jQuery is a function.

But the typical type stops. Unlike statically typed languages, the number, order, modes and types of parameters are not taken into account when calculating the type of function a. In JavaScript, this is simply a β€œfunction”.

When you create a function in JavaScript, the created function object receives two properties: length and prototype , and its prototype has the value Function.prototype , so it inherited properties such as 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 with the "type." Technically, only SIX exists in JavaScript: Null, Undefined, Boolean, Number, String, and Object. So, the real answer to your question, what is the exact jQuery type, is ... actually ... drumroll .... Object .

+6
source

A function is an object and can have properties in Javascript.

See https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function for a list of some properties that a function has by default (and additional properties can be added).

+5
source

Just try to do it yourself and you will understand:

 function f() { } f.prop = '123'; alert(f.prop); 

jQuery is a function , but of course it is also an object that contains its own functions, such as call() and can also have properties.

+3
source

Source: https://habr.com/ru/post/894161/


All Articles