Javascript设计模式之原型模式(五)

创建型设计模式-原型模式

定义

用原型实例指向创建对象的类,使这些类共享原型对象的属性和方法。

原型继承

let PrototypeExtends =function(){
   // 创建缓存类
   let F = function(){};
   for(let i=0 ,length=arguments.length; i<length; i++){
      for(let prop in arguments){
         F.prototype[prop]= arguments[i][item]
      }
   }
   return new F();
};

//测试代码
let SuperMary = PrototypeExtends({
   name:'超级玛丽',
   jump:function(){
      console.log('跳跃技能');
   },
   move:function(step){
      console.log('向前移动'+step + '步');
   },
   eat:function(){
     console.log('吃到毒蘑菇,卒');
   }
});
SuperMary.jump(); // 跳跃技能

猜你喜欢

转载自blog.csdn.net/weixin_39370315/article/details/79798670