1.0.1 • Published 9 years ago

ft-declare v1.0.1

Weekly downloads
9
License
-
Repository
github
Last release
9 years ago

declare.js

declare.js is a function for helping write object oriented javascript code. It is extracted from DOJO.

declare.js用来方便编写面向对象的js代码,其提供类的创建继承/多继承父类方法调用的功能。该方法抽取自dojo中的declare模块,去除了其AOP相关的功能。

安装

npm install ft-declare

加载

支持AMD与CommonJs的方式加载模块

AMD

define(['declare'], function(declare){
  return declare(null, {
    // 创建类
  });
});

CommonJs

var declare = require('ft-declare');
return declare(null, {
  // 创建类
});

创建类

var C1 = declare(null, {
  constructor : function() {
    // 构造函数
    this.name = 'c1';
  },
  sayHi : function() {
    console.log('hi, i am ' + this.name);
  },
  sayHello : function() {
    console.log('hello');
  }
});
var c1 = new C1();

继承类

var C2 = declare([C1], {
  sayHi : function() {
    this.inherited(arguments);
    console.log('hello,bye!');
  }
});
var c2 = new C2();
c2.sayHi();

C2继承自父类C1并复写成员函数sayHi,通过inherited方法可在复写过程中调用父类的同名方法。

多继承

var C3 = declare([C1, C2], {
});

C3有两个父类C1与C2,declare通过实现C3线性算法来给予js多继承能力。

mixin

declare.mixin提供与jQuery.extend相似的功能

1.0.1

9 years ago

0.1.1

9 years ago