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.
See Question&Answers more detail:os