My methods are in Helper
我的方法是在Helper中
var Helper = {
isEmpty: function (obj) {
return !obj || obj === null || obj === undefined || Array.isArray(obj) && obj.length === 0;
},
pushArray: function (arr1, arr2) {
if (arr1 && arr2 && Array.isArray(arr1)) {
arr1.push.apply(arr1, Array.isArray(arr2) ? arr2 : [arr2]);
}
}
}
Question: So, if I have two functions that is isEmpty
(it porposes to check from Array, String, Object) and pushArray
, which of these following three methods should I use to define that functions?. And, What differences are there in three way?
问题:那么,如果我有两个函数isEmpty(它需要检查来自Array,String,Object)和pushArray,我应该使用以下三种方法中的哪一种来定义这些函数?并且,三种方式有什么不同?
There is three way(Maybe, there are other ways.):
有三种方式(也许,还有其他方式。):
Way 1:Array.prototype.someMethod = function(){ ... }
方式1:Array.prototype.someMethod = function(){...}
Way 2:var Helper = {someMethod: function(){ ... }}
方式2:var Helper = {someMethod:function(){...}}
Way 3:window.someMethod = function(){ ... }
方式3:window.someMethod = function(){...}
Personally, I think that:
我个人认为:
- Way1 is not recommended. Because, it's object of ES(ecmascript), not my. Maybe, your method will add by ES in future.
- Way2 is my way which I usually use this. It is global. Also depends it where you use
- Way3 is global way. Also, window is not my object. It doesn't recommend
不建议使用Way1。因为,它是ES(ecmascript)的对象,而不是我的。也许,您的方法将在未来由ES添加。
Way2是我通常使用它的方式。它是全球性的。也取决于你使用的地方
Way3是全球化的方式。此外,窗口不是我的对象。它不推荐
Please, explain with details.(Also, i didn't find such question) Thanks, in advance.
请详细解释。(另外,我没有找到这样的问题)谢谢,提前。
3 个解决方案
#1
1
So, you've proposed two functions that look like they are primarily designed to work with arrays, but they should return intelligent results if you pass them something other than an array.
因此,您提出了两个看起来主要用于处理数组的函数,但如果您传递的不是数组,它们应返回智能结果。
So, right away, you can't use the Array.prototype
method because if the data is not an array, that method won't exist on the object and you won't get the behavior you have currently coded.
因此,您不能立即使用Array.prototype方法,因为如果数据不是数组,则该对象上不存在该方法,您将无法获得当前编码的行为。
So, it really comes down to whether they should be global functions or namespaced functions on your own global object.
因此,它真正归结为它们应该是您自己的全局对象上的全局函数还是命名空间函数。
When in doubt, fewer global symbols is generally the right answer because more global symbols make it more likely you may have a conflict with other code you might include in your project.
如果有疑问,更少的全局符号通常是正确的答案,因为更多的全局符号使您更有可能与项目中可能包含的其他代码发生冲突。
I would suggest this revised implementation of your namespace object:
我建议你修改你的命名空间对象的实现:
var Helper = {
isEmpty: function (obj) {
return !obj || (Array.isArray(obj) && obj.length === 0);
},
pushArray: function (arr1, arr2) {
if (Array.isArray(arr1)) {
if (Array.isArray(arr2) {
// push one array onto the end of the other
arr1.push.apply(arr1, arr2);
} else if (arr2 !== undefined) {
// push a value onto the array
arr1.push(arr2);
}
}
}
}
In isEmpty()
, I've removed the obj === null || obj === undefined
checks because they will never be hit because !obj
will already catch those.
在isEmpty()中,我删除了obj === null || obj === undefined检查,因为它们永远不会被命中,因为!obj已经捕获了那些。
In pushArray()
, I've made it so a falsey value passed in arr2
(such as 0
) can be pushed into the array which your code would not allow.
在pushArray()中,我已经使它在arr2中传递的假值(例如0)可以被推送到你的代码不允许的数组中。
#2
1
Way1 is generally suggested not to use, as in future version's of ecmascript the same function may be introduced which will then be overridden.
Way1通常建议不要使用,因为在ecmascript的未来版本中可能会引入相同的函数,然后将被覆盖。
Way3 is OK to use,but then you are creating a global object directly and as a good practice its good if we have minimum number of global variables.
Way3可以使用,但是你可以直接创建一个全局对象,如果我们有最少数量的全局变量,那么它就是好事。
I prefer u sing Way2 where we encapsulate the utility methods inside a single object for the whole application.Having a single entry point is a good practice,you can create hierarchy inside that . This allows you to have all the custom methods under single variable name and doesnot expose it directly on global namespace as individual one.
我更喜欢你使用Way2,我们将实用程序方法封装在整个应用程序的单个对象中。拥有一个入口点是一个很好的做法,你可以在其中创建层次结构。这允许您将所有自定义方法置于单个变量名称下,并且不会将其作为单独的名称直接在全局名称空间中公开。
#3
0
Array.prototype.someMethod = function() { ... }
This makes it so that all arrays now have someMethod
. That means that if you have the following:
这使得所有数组现在都有someMethod。这意味着如果您有以下内容:
var someArray = [ 1, 2 ];
for (var i in someArray) {
console.log(i);
}
It will print:
它将打印:
0
1
someMethod
This is because, as mentioned earlier, the property someMethod
has been added to all arrays, and now must be filtered out with someArray.hasOwnProperty(i)
. See the jsBin here.
这是因为,如前所述,someMethod属性已添加到所有数组中,现在必须使用someArray.hasOwnProperty(i)过滤掉。在这里查看jsBin。
var Helper = { someMethod: function() { ... } };
This limits the scope of someMethod
to be called with only Helper.someMethod()
.
这限制了仅使用Helper.someMethod()调用someMethod的范围。
window.someMethod = function() { ... };
This makes the someMethod
function global now. This can have unforeseen consequences if you or someone else overwrites the someMethod
function, and can be hard to debug.
这使得someMethod函数现在变为全局。如果您或其他人覆盖someMethod函数,并且可能难以调试,则可能会产生无法预料的后果。
#1
1
So, you've proposed two functions that look like they are primarily designed to work with arrays, but they should return intelligent results if you pass them something other than an array.
因此,您提出了两个看起来主要用于处理数组的函数,但如果您传递的不是数组,它们应返回智能结果。
So, right away, you can't use the Array.prototype
method because if the data is not an array, that method won't exist on the object and you won't get the behavior you have currently coded.
因此,您不能立即使用Array.prototype方法,因为如果数据不是数组,则该对象上不存在该方法,您将无法获得当前编码的行为。
So, it really comes down to whether they should be global functions or namespaced functions on your own global object.
因此,它真正归结为它们应该是您自己的全局对象上的全局函数还是命名空间函数。
When in doubt, fewer global symbols is generally the right answer because more global symbols make it more likely you may have a conflict with other code you might include in your project.
如果有疑问,更少的全局符号通常是正确的答案,因为更多的全局符号使您更有可能与项目中可能包含的其他代码发生冲突。
I would suggest this revised implementation of your namespace object:
我建议你修改你的命名空间对象的实现:
var Helper = {
isEmpty: function (obj) {
return !obj || (Array.isArray(obj) && obj.length === 0);
},
pushArray: function (arr1, arr2) {
if (Array.isArray(arr1)) {
if (Array.isArray(arr2) {
// push one array onto the end of the other
arr1.push.apply(arr1, arr2);
} else if (arr2 !== undefined) {
// push a value onto the array
arr1.push(arr2);
}
}
}
}
In isEmpty()
, I've removed the obj === null || obj === undefined
checks because they will never be hit because !obj
will already catch those.
在isEmpty()中,我删除了obj === null || obj === undefined检查,因为它们永远不会被命中,因为!obj已经捕获了那些。
In pushArray()
, I've made it so a falsey value passed in arr2
(such as 0
) can be pushed into the array which your code would not allow.
在pushArray()中,我已经使它在arr2中传递的假值(例如0)可以被推送到你的代码不允许的数组中。
#2
1
Way1 is generally suggested not to use, as in future version's of ecmascript the same function may be introduced which will then be overridden.
Way1通常建议不要使用,因为在ecmascript的未来版本中可能会引入相同的函数,然后将被覆盖。
Way3 is OK to use,but then you are creating a global object directly and as a good practice its good if we have minimum number of global variables.
Way3可以使用,但是你可以直接创建一个全局对象,如果我们有最少数量的全局变量,那么它就是好事。
I prefer u sing Way2 where we encapsulate the utility methods inside a single object for the whole application.Having a single entry point is a good practice,you can create hierarchy inside that . This allows you to have all the custom methods under single variable name and doesnot expose it directly on global namespace as individual one.
我更喜欢你使用Way2,我们将实用程序方法封装在整个应用程序的单个对象中。拥有一个入口点是一个很好的做法,你可以在其中创建层次结构。这允许您将所有自定义方法置于单个变量名称下,并且不会将其作为单独的名称直接在全局名称空间中公开。
#3
0
Array.prototype.someMethod = function() { ... }
This makes it so that all arrays now have someMethod
. That means that if you have the following:
这使得所有数组现在都有someMethod。这意味着如果您有以下内容:
var someArray = [ 1, 2 ];
for (var i in someArray) {
console.log(i);
}
It will print:
它将打印:
0
1
someMethod
This is because, as mentioned earlier, the property someMethod
has been added to all arrays, and now must be filtered out with someArray.hasOwnProperty(i)
. See the jsBin here.
这是因为,如前所述,someMethod属性已添加到所有数组中,现在必须使用someArray.hasOwnProperty(i)过滤掉。在这里查看jsBin。
var Helper = { someMethod: function() { ... } };
This limits the scope of someMethod
to be called with only Helper.someMethod()
.
这限制了仅使用Helper.someMethod()调用someMethod的范围。
window.someMethod = function() { ... };
This makes the someMethod
function global now. This can have unforeseen consequences if you or someone else overwrites the someMethod
function, and can be hard to debug.
这使得someMethod函数现在变为全局。如果您或其他人覆盖someMethod函数,并且可能难以调试,则可能会产生无法预料的后果。