Is it possible to give javascript partial class behavior like C# or monkey patching like Ruby does?

前端 未结 5 1773
感动是毒
感动是毒 2021-01-01 05:14

The idea behind partial classes is that you can group certain functions together. The best example of this in C# is putting control definitions in one file and the event han

5条回答
  •  悲&欢浪女
    2021-01-01 06:01

    Redefine the type through inheritance approach like this:

    var Dog = Class.extend({
        init:function(data){
             data = data || {};
             this.name = data.name;
        },
        run:function(){ 
            /*impl*/
        }
    });
    
    /*other js file that require first file */
    var Dog = Dog.extend({
       init:function(data){ 
           this._super(data); 
       },
       bark:function(){ 
           return 'woof';
       }
    });
    

    The only problem in this approach is the dependency management, but it works.

    obs: using John Resing class.js, but could be written in typescript, ES6, AngularJs, ... and many other libraries.

提交回复
热议问题