JavaScript Extending Class

前端 未结 10 1372

I have a base class:

function Monster() {
  this.health = 100;
}

Monster.prototype.growl = function() {
  console.log(\"Grr!\");
}

That

10条回答
  •  北海茫月
    2020-11-30 18:36

    If you don't like the prototype approach, because it doesn't really behave in a nice OOP-way, you could try this:

    var BaseClass = function() 
    {
        this.some_var = "foobar";
    
        /**
         * @return string
         */
        this.someMethod = function() {
            return this.some_var;
        }
    };
    
    var MyClass = new Class({ extends: BaseClass }, function()
    {
        /**
         * @param string value
         */
        this.__construct = function(value)
        {
            this.some_var = value;
        }
    })
    

    Using lightweight library (2k minified): https://github.com/haroldiedema/joii

提交回复
热议问题