How do you structure your jQuery code?

Based on mootools and JAVA, the implementation of the mootools class is a great way to structure my code, plus I can have some interesting functions, such as extension, implementation, etc. Starting with jquery, I found that I myself am writing $ .fn plugins that cannot use the code of other plugins. In addition, there is no idea to use the plugin structure for complex things that are not very relevant to DOM Elements. Is there a better way than $ .fn? What do you recommend structuring your code with jquery.

+3
source share
4 answers

This is a difficult question to answer.

JavaScript , .

JavaScript ( , Moo, Joose, JS.Class, Base2 ..) , - JavaScript, .

, jQuery "". - , $() jQuery(). jQuery, , , . , , "" JavaScript.

, JS, , . JS ( ). ( ).

, Classes for.

jQuery Ariel Flesler jQuery.Collection . "" , DOM jQuery.

Underscore.js, .

+6

.

, , :

Function.prototype.derive = (function() {
    function Dummy() {}
    return function() {
        Dummy.prototype = this.prototype;
        return new Dummy;
    };
})();

function Base() {}
function Sub() {}
Sub.prototype = Base.derive();

. :

// create package:
var foo = new (function() {
    // define package variables:
    this.bar = 'baz';
    this.spam = 'eggs';

    // export package variables:
    this.exports = (function(name, obj) {
        var acc = [];
        for(var prop in obj) {
            if(obj.hasOwnProperty(prop))
                acc.push(prop + '=' + name + '.' + prop);
        }
        return 'var ' + acc.join(',') + ';';
    })('foo', this);
});

// use package:
(function() {
    // import package variables:
    eval(foo.exports); 
    alert(spam);
})();
+3

jQuery . , , .

, . jQuery, DOM, Ajax, - ..

+2

, , .prototype, "", , .

, , .

, , javascript, jQuery , , , , . , onclick .bind('click',...), , .

+1

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


All Articles