dropzone.js: __slice = [].slice
dropzone.js: __slice = [].slice
我正在努力弄清楚 enyo 的精彩部分 dropzone.js。我对 JavaScript 思路和原型范式相当陌生。第四行有一行代码,我想知道它的作用以及为什么要使用这样一行代码。该行如下所示:
var __slice = [].slice
我的理解是否正确,这从数组原型对象中获取切片函数,只是提供了一种更简单的方式来引用它?这种方法有什么好处?为什么开头是双下划线?我在某处读到,这是为了避免全局范围内的冲突,但这不是已经通过将整个代码包装在
中来避免了吗?
(function() { //code here
}).call(this);
构建?
Have I understood correctly, that this takes the slice
function from the array prototype object and just gives it a bit easier way to refer to it?
是的。
What is the benefit of this approach?
__slice
比 Array.prototype.slice
短(这还需要额外的几层 属性 查找),理论上 [].slice
创建并丢弃一个对象.因此,拥有一个标识符只是节省了一些输入和最微小的运行时性能。
And why the double-underscore at the beginning? I read somewhere, that this would be to avoid conflicts in the global scope, but isn't this already avoided by wrapping the whole code in the ...
是的,这样做已经避免了。在这种情况下,这只是作者想要使用的约定,也许是为了表明这是 Array.prototype.slice
的 his/her 快捷方式。 __
没有内在含义。
我正在努力弄清楚 enyo 的精彩部分 dropzone.js。我对 JavaScript 思路和原型范式相当陌生。第四行有一行代码,我想知道它的作用以及为什么要使用这样一行代码。该行如下所示:
var __slice = [].slice
我的理解是否正确,这从数组原型对象中获取切片函数,只是提供了一种更简单的方式来引用它?这种方法有什么好处?为什么开头是双下划线?我在某处读到,这是为了避免全局范围内的冲突,但这不是已经通过将整个代码包装在
中来避免了吗?(function() { //code here
}).call(this);
构建?
Have I understood correctly, that this takes the
slice
function from the array prototype object and just gives it a bit easier way to refer to it?
是的。
What is the benefit of this approach?
__slice
比 Array.prototype.slice
短(这还需要额外的几层 属性 查找),理论上 [].slice
创建并丢弃一个对象.因此,拥有一个标识符只是节省了一些输入和最微小的运行时性能。
And why the double-underscore at the beginning? I read somewhere, that this would be to avoid conflicts in the global scope, but isn't this already avoided by wrapping the whole code in the ...
是的,这样做已经避免了。在这种情况下,这只是作者想要使用的约定,也许是为了表明这是 Array.prototype.slice
的 his/her 快捷方式。 __
没有内在含义。