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

前端 未结 5 1775
感动是毒
感动是毒 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条回答
  •  萌比男神i
    2021-01-01 06:08

    If such a split makes real sense then you can do this:

    File #1

    function MyObjectType() { this.init(); }
    

    File #2

    MyObjectType.prototype.init = function() { this.one = 1; }
    

    File #3

    MyObjectType.prototype.onClick = function() { if(this.one == 1) alert("I am so alone..."); }
    

    And somewhere else you can use it as:

    var myObject = new MyObjectType();
    myObject.onClick();
    

    Welcome to prototype yet kinda functional programming world!

提交回复
热议问题