Design Pattern Learning - (2. Inheritance)

I reviewed it myself.


For details, please see the previous blog, click here .


About polymorphism:

As we all know, in other languages ​​such as c, c++, csharp, java, etc., in addition to encapsulation, inheritance, there is also a polymorphism.

Although js does not emphasize this, it is still possible to realize this function in disguise. It is very simple and can be understood at a glance.


  var objadd = function () {};
    objadd.prototype.add = function () {
        var arg = arguments,
                len = arg.length;
        switch( len ){
            // no parameters
            case 0:
                return 10;
            case 1:
                return 10 + arg[0];
            case 2:
                return arg [0] + arg [1];
            // etc
        }
    };
    var obj = new objadd();
    console.log( obj.add() );
    console.log( obj.add( 1 ) );
    console.log( obj.add( 10 ,15  ) );

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326019043&siteId=291194637