解读开源js函数的收获

来源:互联网 发布:2017网络营销发展数据 编辑:程序博客网 时间:2024/06/06 03:02

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

0 0