What is the easiest way to create an anchored JavaScript framework like jQuery?

I would like to write a little JavaScript (framework) that can associate functions with all of the following functions that know the data of their predecessor.

In principle, I'm not interested in bloating (I understand that it is small, for my even smaller project, although it is bloating), which provides jQuery, but would like to imitate some of its behavior - primarily for educational purposes and the availability of data for all related functions.

I would like, for example, to do something like:

myJsLib.processForm("user", "pass").url("http://domain.dev/form.php").queryString({ secure: true, errorPage: "login_failure.htm" });

In the above example, all functions should be somewhat aware of what the other is doing.

Or, more precisely:

myJsLib.getDataIntoArray(jsonObjectOrWhatever).each(function(item) { alert(item); });

Where "item" will be the array created by getDataIntoArray () (and returned?).

, . . jQuery , JavaScript. , .

.

EDIT: , -, . , , , .

function myLib()
{
this.properties = ['status', 'window', 'ui'],
this.inputArrayParms = function(parms)
{
    for (var i = 0, len = parms.length; i < len; i++)
    {
        this.properties[this.properties.length] = parms[i];
    }
    return this;
},
this.each = function(callback)
{
    for (var i = 0, len = this.properties.length; i < len; i++)
    {
        callback(this.properties[i]);
    }
    return this;
}
}

var f = new myLib;
f.inputArrayParms(["one", "two", "three"]).each(function(theMsg){ alert(theMsg); });

, , . ?

+3
1

, - (, jQuery), , .

:

function foo() {
    this.first = function(first) {
        alert(first);
        return this;
    }
    this.second = function(second) {
        alert(second);
        return this;
    }
}

, foo : first second. this, :

new foo().first("hello").second("world");
new foo().second("hello").first("world");
new foo().first("hello").first("world");

:)

, , , . :

f = new foo();
f.first("hello");
f.second("world");

, - , , this, . , , , .

+5

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


All Articles