1
我做了很多jQuery特殊事件,現在我試圖讓它們成爲jQuery插件。在其他對象上使用本機數組原型方法的風險?
我的代碼是:
$.each("down move hold up tap swipeUp swipeDown swipeLeft swipeRight drag dragCell".split(" "), function(key, value) {
$.fn[value] = function() {
var args = [value].concat(Array.prototype.splice.call(arguments,0));
if ($.isFunction(arguments[0]) || $.isFunction(arguments[1])) {
return this.bind.apply(this, args);
} else {
return this.trigger.apply(this, args);
}
};
});
哪裏迴路名稱(向下,移動,持有,向上等)的陣列,並動態生成的jQuery插件。
正如你可以看到一個拿這個值後命名插件:$.fn.down
,$.fn.move
等。
然後我接受的參數和循環是通過本機陣列的方法。
Array.prototype.splice.call(arguments, 0);
這將返回本地陣列[arguments[0], arguments[1], ...]
並與value
concat
它,並結束了:
["down", arguments[0], arguments[1], ...],
["move", arguments[0], arguments[1], ...],
....
是否有這樣做的風險(使用本機方法和其它物體,然後意思),並且有沒有可能丟失數據?
正如你可以看到它與jQuery的bind
和trigger
方法混淆。這也是危險的嗎?