In js libraries like $ .something (object) can also be $ ("something") (function)?

This may turn out to be a simple question with a very complex answer or with something very simple that I just missed completely, but in libraries like Prototype and jQuery etc., like a global variable, $an object that contains functions, for example:

$.ajax.get(…);

as well as the function itself, for example:

$("…");

Any help, even a pointer to a more explanatory website would be brilliant, I'm just intrigued!

For example, if I created a function ( $) containing a child object ( ajax), for example:

var $ = function() {
    this.ajax = {};
    return this;
}; 

I could access an object ajax, for example $().ajax, but don't like $.ajaxhow you get them?

+3
3

JavaScript - , , !

var f = function(x) { return x; };
f.attr = "a";
f.attr; // => "a"
f(123); // => 123
+7

javascript - javascript.

var obj = {};
var func = new function(){};

obj.property = "";
func.property = "";

: http://ejohn.org/apps/learn/#16

+3
var $ = (function () {

    var keys = { a: 1, b: 2, c: "Bob" },
        external_func = function (key, val) {
            if (val === undefined) { return keys[key]; }
            keys[key] = val;
            extend(external_func, { key : val });
        };

    function extend (obj, keys) {
        var key;
        for (key in keys) {
            if (keys.hasOwnProperty(key)) {
                obj[key] = keys[key];
            }
        }
    }

    extend(external_func, keys);
    external_func.setKey = function (key, val) { keys[key] = val; this[key] = val; };
    return external_func;
}());

jQuery , , , , , , .

, URL- cookie , - :

( " " ); Cookie [ " " ]; Cookie.username;

( "_" ); [ "_" ]; Query.page_name;

You just have to be sure that you treat it like reading, unless you use this function, use the method setCookieattached to this function, or you focus only on browsers that support their own getters and setters.

0
source

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


All Articles