js templates  →  Javascript Шаблоны

published 10 March 2012

{"#" * i} Циклы:

For

var i, myArray =[];

for(i = myArray.length; i--;) {
    myArray[i];
}

While

var myArray,
     i = myArray.length;
while(i--) {
    myArray[i]; 
}

{"#" * i} Комментарии:

Application:

/**
 * My JS application
 *
 * @module
 */
MyApp = {};

Class:

/**
 * A math utility 
 * @namespace MyApp
 * @class my_stuff
 */
MyApp.math_stuff = {};

Method:

/**
 * Sums two numbers
 *
 * @method sum
 * @param {Number} a First number
 * @param {Number} b The second number
 * @return {Number} The sum of the two inputs
 */
sum : function (a, b) {
    return a + b;
}

Конструктор

/**
 * Constructs Person object
 * @class Person
 * @constructor
 * @namespace MyApp
 * @param {String} first First name
 * @param {String} last Last name
 */
MyApp.Person = function (first, last) {
   /**
    * Name of the person
    * @property last_name
    * @type String
    */
    this.first_name = first;
    /**
    * Last (family) name of person
    * @property last_name
    * @type String
    */
    this.last_name = last;
}
}

{"#" * i} Обратный вызов

// измененный вариант функции findNodesO, 
// принимающий функцию обратного вызова 
var findNodes = function (callback) { 
    var i = 100000, 
    nodes = [], 
    found; 
    // проверить, является ли объект callback функцией 
    if (typeof callback !== "function") { 
        callback = false; 
    } 
    while (i) { 
        i -= 1; 
    // здесь находится сложная логика выбора узлов... 
    // теперь вызвать функцию callback: 
    if (callback) { 
        callback(found); 
    } 
    nodes.push(found); 
    } 
    return nodes; 
};

{"#" * i} Модуль

MYAPP = {};
MYAPP.utilits = {};
MYAPP.utilits.array = (function(){
    //
    var app = 5,
    app2 = 6;
    return {
        inArray : function (a, b) {
           return app + '1'; 
        },
        isArray : function (a, b) {
            return app2 +'2';
        }
    }
}());
MYAPP.utilits.array2 = (function(){
    //
    var app = 5,
    app2 = 6; 
    inArray = function (a, b) {
        return app + '1'; 
    },
    isArray = function (a, b) {
        return app2 +'2';
    };
    
    return {
       isArray : isArray,
       inArray : inArray
    };
}())

{"#" * i} Константы

var constant = (function(){
    var constants = {},
    ownProp = Object.prototype.hasOwnProperty,
    allowed = {
        string : 1,
        number : 1,
        boolean : 1
    },
    prefix = (Math.random() + "_").slice(2);
    return {
        set : function (name, value) {
            if(this.isDefined(name)) {
                return false;
            }
            if(!ownProp.call(allowed, typeof value)) {
                return false;
            }
            constants[prefix + name] = value;
            return true;
        },
        
        isDefine : function (name) {
            return ownProp.call(constants, prefix + name);
        },
        
        get : function (name) {
            if(this.isDefine (name)) {
                return constants[prefix + name];
            }
            return null;
        }
    }
})
Теги: js templates