Deathmatch: Self Executing Anonymous Function -vs- “new function”

前端 未结 3 2096
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-08 08:17

Answer Embedded Below in UPDATE 2/ANSWER

Thanks to Joseph for helping me find the answer (even though I don\'t like it =).

ORIGINAL

相关标签:
3条回答
  • 2020-12-08 08:25

    For one thing, this pattern:

    MyNamespace.UsingNew = new function() {
        var fnPrivate = function() {
    
            //what's this in here?
    
            return "secrets1";
        };
    
        this.property = "value1";
        this.method = function() {
    
            //what's this in here?
    
            return "property = " + this.property + ' ' + fnPrivate();
        }
    };
    
    • uses the "new" keyword to create an instance of an object, which is modeled using a constructor function. forgetting to use "new", you'll end up named making a function MyNamespace.UsingNew() instead of an object.

    • it's a constructor function, and not yet an object's instance (until you build it using new). you need to use "this" to pertain the object that it will become. it just add's problems to scope, especially when you nest more functions in it, and the value of "this" will change from time to time (and you won't see it coming until the console tell's you). familiar with the self=this or that=this to save the value of "this"? it's pretty much seen when using this pattern

    on the otherhand, the next pattern is somewhat better (for me) since:

    • you don't need to use "new" since it returns an object.

    • not using "this" since it already returns an object. you don't even need "this".

    also, i prefer constructing the other pattern this way:

    ns.myobj = (function(){
    
        //private
        var _privateProp = '';
        var _privateMeth = function(){};
    
        //public
        var publicProp = '';
        var publicMeth = function(){};
    
        //expose public
        return {
            prop:publicProp,
            meth:publicMeth
        };
    }());
    
    0 讨论(0)
  • 2020-12-08 08:25

    Both are almost identical and should have relatively the same performance characteristics. It is just a matter of style. I, personally, favor the second one but it is written a bit funky. I would write it,

    MyNamespace.UsingSelfEx = (function () {
        function fnPrivate() {
            return "secrets2";
        }
        return {
            property: "value2";
            method: function () { 
                return "property = " + this.property + " " + fnPrivate();
            }
        }
    })();
    

    You don't really need the parens around the function but self-executing functions normally have them and lets the reader know, at the beginning, that this is a self executing function.

    0 讨论(0)
  • 2020-12-08 08:42

    I've seen this and I liked it

    var MyThing = (function(){
    
        function MyThing(args){
        }
    
        MyThing.prototype = {
            prototypeMethod: function() {}
        };
    
        return MyThing;
    })();
    

    You can also do this

    MyThing.create = function(args){
        return new MyThing(args);
    }
    

    Usually it is enclosed in an another self invoking function. In order to avoid naming collisions.

    (function({
    
    })(window);
    

    And the window object is passed as an argument, to assign it to an upper scope.

    window.MyThing = MyThing;
    

    In this example you can use, static variables,private etc.

    0 讨论(0)
提交回复
热议问题