javascript继承结构设计

      本人博客站点:http://www.zeromike.net

      本文地址:http://www.zeromike.net/?p=35

      场景:有多个应用处理方式类似,一打开应用就先初始化界面信息,然后各自又有各自的业务逻辑处理方式。所以我在这里抽象出来一个基类,多个应用继承这个基类,这里使用prototype(原型)方式实现继承。

     

//基类
function App(){
  
}
App.init=function(){
    console.log("App init...");
}
App.init();

App.create = function(app){
    if (app instanceof App){
      app.init();
    }
}
//CutFruit
function CutFruit(){
  this.init=function(){
    console.log("CutFruit init...");
  }
  this.cut=function(){
    console.log("Your finger speed is very fast...");
  }
}
//继承App
CutFruit.prototype = new App();
//CatchThief
function CatchThief(){
  this.init=function(){
    console.log("CatchThief init...");
  }
  this.fire=function(){
    console.log("fire in the hole....");
  }
}
//继承App
CatchThief.prototype = new App();

function main(){
  var classRF = window[arguments[0]];
  App.create(new classRF());
}
main("CutFruit");
main("CatchThief");
(new CutFruit()).cut();
(new CatchThief()).fire();

   打印结果:

   App init...

   CutFruit init...

   CatchThief init...

   Your finger speed is very fast...

   fire in the hole....

你可能感兴趣的:(JavaScript)