解讀開源js函數的收穫

JS活用

>

代碼

var t, n, e, r, a;
for(t = {},
 a = "Boolean Number String Function Array Date RegExp Undefined Null".split(" "),
 n = a.length;
  n > r; r++)
    e = a[r],
    t["[object " + e + "]"] = e.toLowerCase();

StrObject.split(” “):分割字符串;“,”替換“指定字符串”;
StrObject.toLowerCase():字符串小寫 ;
“,” “||” “&&” 三種運算符的使用;
禁止選中

//js    弊端。只對該對象生效。外部仍會觸發
                oncontextmenu="return false;" //禁止鼠標右鍵
                ondragstart="return false;" //禁止鼠標拖動
                onselectstart="return false;"//禁止選中文字
                onselect="document.selection.empty();"//禁止複製文本
//D3
                a
                .attr("onselectstart",function () {
                    return "return false";
                })
                .attr("onmousedown", function () {
                    return "return false";
                })
                .attr("ondragstart",function () {
                    return "return false";
                })

深拷貝

        function getType(o) {
            var _t;
            return((_t = typeof(o)) == "object" ? o == null && "null" || Object.prototype.toString.call(o).slice(8, -1) : _t).toLowerCase();
        }```

     function extend(destination, source) {
            for(var p in source) {
                console.log(p);
                if(getType(source[p]) == "array" || getType(source[p]) == "object") {
                    destination[p] = getType(source[p]) == "array" ? [] : {};
                    arguments.callee(destination[p], source[p]);
                } else {
                    destination[p] = source[p];
                }
            }
        }

s

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章