Own "module" with functions inside

Discussion in 'Learning Javascript' started by Milena, Nov 14, 2015.

    Tags:
  1. Milena

    Milena The woman of many questions Veteran

    Messages:
    1,282
    Likes Received:
    106
    Location:
    Ireland
    First Language:
    Irish
    Primarily Uses:
    N/A
    In ace, we have something like this:

    module Sample def initialize #stuff here endendif we translate this in JS, how do we exactly do it?
     
    #1
  2. Iavra

    Iavra Veteran Veteran

    Messages:
    1,797
    Likes Received:
    856
    First Language:
    German
    var Sample = {


    initialize: function() {


    // stuff here


    }


    };
     
    #2
    Milena likes this.
  3. Milena

    Milena The woman of many questions Veteran

    Messages:
    1,282
    Likes Received:
    106
    Location:
    Ireland
    First Language:
    Irish
    Primarily Uses:
    N/A
    Thanks. Works perfectly :D
     
    #3
  4. DarknessFalls

    DarknessFalls Rpg Maker Jesus - JS Dev. Veteran

    Messages:
    1,393
    Likes Received:
    209
    First Language:
    English
    // Preferred way (for me)class x {}module.exports = x;or:

    // ES5 "class" wayvar x = {   functionName: function(){}}module.exports = x;or

    // prototypesfunction something(){}something.prototype.constructor = function() {};module.exports = something;Notice the 

    module.exports = whatEver;it creates a module that is then not global, in the sense that everything is scoped to that module. You can then use something like require to require your modules in to other modules:

    require('./whatEver')whatEver here is the name of the file containing the module. 

    These are es6 concepts How ever these are simple concepts to understand. They do require something like browserify with the bablify extension to compile in to es5 code. The code will be but ugly, but thats what github is for.

    This is the true concept of modules.

    If you want something like yanfly:

    var Imported = Imported || {};Imported.YEP_BaseTroopEvents = true;var Yanfly = Yanfly || {};Yanfly.BTE = Yanfly.BTE || {};You create with objects with subjects that hold the information you want, how ever I have never seen this done in web development .... this:

    var Yanfly = Yanfly || {};I've seen done and I tend to refactor the code to move on to the actual concept of "modules"
     
    #4
    Milena likes this.

Share This Page