How to create a function and call its methods without using new ones similar to jQuery $?

$jQuery is a function, however you can access its methods as if it were an object through ".". Can someone explain how I can do this in a function funcfor example?

enter image description here

It is more clear what I intend to use func ()as well func.each (), for example.

+4
source share
1 answer

In javascript, functions are objects; you can attach properties to them:

function foo() {
    console.log("foo is called!");
}

foo.prop = "Hello, world!";

foo.boo = function() {
    console.log("foo.boo is called!");
}

foo();                  //=> "foo is called!"
console.log(foo.prop);  //=> "Hello, world!"
foo.boo();              //=> "foo.boo is called!"
Run codeHide result

: , $.each !== $(...).each. - $, jQuery. ( , , ).

+6

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


All Articles