跳到主要內容

全域變數於JS的使用

有時為了求資料讀取速度與方面性, 真的很難避免全域變數的使用, 若該變數內容不具隱私權, 又不會被任意更動, 個人是覺得可以使用, 提供簡單的func如下, 將變數內容新增成object, 又給予id辨別, 全域變數只會被讀取不會被改變...

HTML 宣告全域變數


JS 處理全域變數

//MODEL
var MODEL = (function () {
      // Create a store to hold the private objects.
    var privateStore = {}; 
    var uid = 0;
    function MODEL (data) {
        privateStore[this.id = uid++] = {}; 
        privateStore[this.id].data = data || ""; 
    }   
    MODEL.prototype.readData = function (item) {
        var data = privateStore[this.id].data;
        if (item && data.hasOwnProperty(item)) {
            return data[item];
        } else {
            return data;
        }   
    };  
    return MODEL;
}());

var model = new MODEL(dataTest);
model = model.readData();
//model could be change not infect global variable

留言

這個網誌中的熱門文章

Indexed Database API - Initail

As a front web developer, I could create UI faster or Optimize file to load page faster. But it hard to faster response time form server however 80-90% of the end-user response time is spent on the frontend. Nowadays we could use the Indexed Database API, or IndexedDB to reduce the request times. Less request times, less response time. What is IndexedDB ? IndexedDB is web browser standard interface for a local database of records holding simple values and hierarchical objects. IndexedDB was initially proposed by Oracle in 2009./* wiki */ What browsers would support IndexedDB ? Most browsers of hybrid devices support it exclude IE9-, Safari 7-,iOS Safari 7.1-, Opera Mini, Android Browser 4.3-./* caniuse.com */ When shall we use IndexedDB ? Save Money on web hosting Data index sort Data keyword search Keep data for period of time How to start it? //*************************************************************** //initDB //{Object}, {data: dbName, objectStore: storeName, ...

最近設計的一組icon..太有APPLE風格被打槍了

jQuery 的slideToggle太好用了

$ ( ".collapse" ) .each ( function ( index, value ) { $ ( this ) . click ( function ( evt ) { evt.preventDefault ( ) ; //prevent the default click event of the anchor tag. var target = $ ( this ) .attr ( "data-toggle" ) ; $ ( "." + target ) .slideToggle ( ) ; } ) ; } ) ; http://code-tricks.com/create-a-common-toggler-for-div-using-jquery/