-2

I know how to build a library in Javascript, but it is very different in jQuery.

// what I know
 var Sambuca;
Sambuca = function(selector) {

    var self = {};
    self.selector = selector;
    self.elements = (typeof selector === 'object') ?
        new Array(self.selector) :
        document.querySelectorAll(self.selector);

    //Our functions will be put here

    return self;

};

window.Sambuca = Sambuca;
window.$ === undefined && (window.$ = Sambuca)
  1. I want to know what is this code in jQuery lib file?
  2. How do I add my own functions, and how do I get selector and how used in Javascript files?
  3. What does this code refer to?
// in jQuery
    !function (e, t) {
            "use strict";
            "object" == typeof module && "object" == typeof module.exports ? module.exports = e.document ? t(e, !0) : function (e) {
                if (!e.document) throw new Error("jquery module requires a window with a document");
                return t(e)
            } : t(e)
        }("undefined" != typeof window ? window : this, function (C, e) {
            "use strict";
            
          
    });

Answer Nothing seek .nothing find. (thx stackoverflow)

with help ADJenks i found a great help,maybe it will help you

link here : https://gist.github.com/CrocoDillon/9990078

H.jalali
  • 123
  • 5
  • Look at what babeljs does to this simple class: https://babeljs.io/repl#?browsers=defaults%2C%20not%20ie%2011%2C%20not%20ie_mob%2011&build=&builtIns=false&spec=false&loose=false&code_lz=KYDwDg9gTgLgBAE2AMwIYFcA28DGnUDOBcAChDKjBHAN4BQccOEAdgTFOjlVABQCU9RnACQMABYBLAgDoCkgF7A4AXjgAGOAzgBfbQHMoEAO4ChjMVNnylAaltbGenUA&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=env%2Ces2015%2Creact%2Cstage-2&prettier=false&targets=&version=7.12.3&externalPlugins= – ADJenks Oct 22 '20 at 19:11
  • It takes the most modern JavaScript and "transpiles" it down to work in a much older JavaScript engine. – ADJenks Oct 22 '20 at 19:13

1 Answers1

0

They're trying to make it compatible with module loaders. See: https://github.com/umdjs/umd Compare UMD to AMD and to CommonJS.

I recommend using the modern module syntax. Like here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules

Then use babeljs or some other transpiler to transform it to something more compatible with older browsers.

ADJenks
  • 1,787
  • 17
  • 26