javascript继承框架 [英] javascript inheritance framework

查看:79
本文介绍了javascript继承框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在一个小型,轻量级的javascript类继承解决方案,该解决方案在客户端和服务器端(node.js)上都能正常工作?我不想要一个大的库,只是想要一个可以让我声明构造函数和一些方法,然后让类能够继承它的功能的库.

Is there a small, lightweight solution for javascript class inheritance that will work well on both client and server side (node.js)? I'm not wanting a big library, just something that will allow me to declare a constructor and some methods, then have the ability for a class to inherit that.

推荐答案

John Resig在大约25行代码中概述了一个简单的继承框架

John Resig outlines a simple inheritance framework in about 25 lines of code here. I have seen it used to good effect.

您可以像这样使用它:

var Vehicle = Class.extend({
  init: function(wheels) {
    this.wheels = wheels;
  }
});

var Truck = Vehicle.extend({
  init: function(hp, wheels) {
    this.horsepower = hp;
    this._super(wheels);
  },

  printInfo: function() {
    console.log('I am a truck and I have ' + this.horsepower + ' hp.');
  }
});

var t = new Truck(4, 350);
t.printInfo();

这篇关于javascript继承框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆