Constructors in JavaScript objects

后端 未结 19 1718
夕颜
夕颜 2020-11-22 10:21

Can JavaScript classes/objects have constructors? How are they created?

相关标签:
19条回答
  • 2020-11-22 10:58

    This pattern has served me well. With this pattern, you create classes in separate files, load them into your overall app "as needed".

    // Namespace
    // (Creating new if not instantiated yet, otherwise, use existing and just add to it)
    var myApp = myApp || {};
    
    // "Package" 
    // Similar to how you would establish a package in other languages
    (function() {
    
    // "Class"
    var MyClass = function(params) {
        this.initialize(params);
    }
    
        // "Private Static" vars 
        //    - Only accessible to functions in this class.
        //    - Doesn't get wiped out when we create a new instance.
        var countInstances = 0;
        var allInstances = [];
    
        // "Private Static" functions 
        //    - Same as above, but it's a function accessible 
        //      only to other functions in this class.
        function doSomething(){
        }
    
        // "Public Static" vars
        //    - Everyone has access.
        //    - Doesn't get wiped out when we create a new instance.
        MyClass.counter = 0;
    
        // "Public Static" functions
        //    - Same as above, but anyone can call this "static method".
        //    - Kinda like a singleton class situation.
        MyClass.foobar = function(){
        }
    
        // Public properties and methods are built into the "prototype"
        //    - This is how each instance can become unique unto itself.
        //    - Establishing "p" as "local" (Static Private) variable 
        //      simply so we don't have to keep typing "MyClass.prototype" 
        //      for each property and function.
    var p = MyClass.prototype;
    
        // "Public" vars
        p.id = null;
        p.firstname = null;
        p.lastname = null;
    
        // "Private" vars
        //    - Only used by "this" instance.
        //    - There isn't "true" privacy for each 
        //      instance so we have to fake it. 
        //    - By tradition, we indicate "privacy"  
        //      by prefixing it with an underscore. 
        //    - So technically, anyone can access, but we simply 
        //      don't tell anyone about it (e.g. in your API)
        //      so no one knows about it :)
        p._foo = null;
    
        p.initialize = function(params){
            this.id = MyClass.counter++;
            this.firstname = params.firstname;
            this.lastname = params.lastname;
            MyClass.counter++;
            countInstances++;
            allInstances.push(this);
        }
    
        p.doAlert = function(theMessage){
            alert(this.firstname + " " + this.lastname + " said: " + theMessage + ". My id:" + this.id + ".  Total People:" + countInstances + ". First Person:" + allInstances[0].firstname + " " + allInstances[0].lastname);
        }
    
    
    // Assign class to app
    myApp.MyClass = MyClass;
    
    // Close the "Package"
    }());
    
    // Usage example:
    var bob = new myApp.MyClass({   firstname   :   "bob",
                                    lastname    :   "er"
                                });
    
    bob.doAlert("hello there");
    
    0 讨论(0)
提交回复
热议问题