Last Updated: September 29, 2021
·
2.485K
· jjperezaguinaga

Javascript Template for Libraries

Introduction

Lately I had been struggling trying to explain Javascript to newcomers, specially to people that come from OOP language and have problems understanding the language. As a result, I created a simple template for them to start hack away. As a bonus, I also added the basics for YuiDocs and how to use it.

Here's the stripped template. I would go check the Gist, as it seem the Syntax Highlighting works funky, plus I added some explanations there.

/**
* A simple template for native Javascript modules and classes with documentation
* @module Template
* @author jjperezaguinaga
* @copyright (c) 2012 Company Inc.
* @requires NoLibrary
**/

window.Template = (function( t, undefined ) {

/**
* My awesome class that does funky things.
* @class AwesomeClass
* @constructor
* @chainable
**/
t.AwesomeClass = function() {
    var self = {};

    /**
    * My awesome property. Stick to a code standard - under_score or camelCase!
    * @property awesomeProperty
    * @type = {String}
    * @default "" 
    **/
    self.awesomeProperty = "";

    /**
    * My awesome private property. Closures! Your object has scope on this variable.
    * @property id
    * @type = {Number}
    * @default 0
    * @private
    *
    **/
    var id = 0;

    /**
    * "Constructor" of the class. Don't confuse with the at-constructor block
    * @method init
    * @param {String} awesome The initial value of our awesome property
    * @param {Number} id Our value id!
    **/
    self.init = function(awesome, id) {
        self.awesome = awesome;
        id = id;
        return self;
    }

    return self.init();
}

return t;
})(window.Template || {});

Here's the Gist, feel free to grab it/fork it/comment. I also created a Github Project to show more less the way I use it. In practice, though, I usually have an extra module that manages all the dependencies.

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside