问题
My methods are in 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?
There is three way(Maybe, there are other ways.):
Way 1:Array.prototype.someMethod = function(){ ... }
Way 2:var Helper = {someMethod: function(){ ... }}
Way 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
Please, explain with details.(Also, i didn't find such question) Thanks, in advance.
回答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.
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.
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.
回答2:
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.
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.
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.
回答3:
Array.prototype.someMethod = function() { ... }
This makes it so that all arrays now have someMethod. That means that if you have the following:
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.
var Helper = { someMethod: function() { ... } };
This limits the scope of someMethod to be called with only Helper.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.
来源:https://stackoverflow.com/questions/32356663/which-ways-are-best-to-define-my-method