js圣杯模式

//圣杯模式 改变子属性不会影响父对应的属性
    // function inherit(Target, Origin) {
    //   function F() { }
    //   F.prototype = Origin.prototype
    //   Target.prototype = new F()
    //   Target.prototype.constructor = Target
    // }
    var inherit = (function () {
      var F = function () { };
      return function (Target, Origin) {
        F.prototype = Origin.prototype
        Target.prototype = new F()
        Target.prototype.constructor = Target
      }
    }())
    Father.prototype.lastName = "haha"
    function Father() {
    }
    function Son() {
    }
    inherit(Son, Father)
    var son = new Son()
    var father = new Father()

 

posted @ 2024-05-08 18:44  howhy  阅读(8)  评论(0编辑  收藏  举报