Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

javascript - Which ways are best to define my method.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...