Skip to content

Commit

Permalink
..
Browse files Browse the repository at this point in the history
  • Loading branch information
crossjs committed Oct 30, 2014
1 parent 993c9bf commit 37a13db
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 22 deletions.
38 changes: 19 additions & 19 deletions class.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ Class.superclass = Class.prototype = {
* 扩展实例方法/属性
* @method extend
* @param {Object} obj1 实例方法集
* @param {Object} [objN] 实例方法集
* @return {Object} 类实例
* @param {object} [properties] 实例方法集
* @param {object} [properties] 实例方法集
* @return {object} 类实例
*
* @example
* ```
Expand All @@ -78,7 +78,7 @@ Class.superclass = Class.prototype = {
* // I'm 13 years old.
* ```
*/
extend: function( /*obj1[, objN]*/ ) {
extend: function( /*[properties[, ... properties]]*/ ) {
Array.prototype.unshift.call(arguments, this);

mixin.apply(null, arguments);
Expand All @@ -93,10 +93,10 @@ Class.superclass = Class.prototype = {
* @method create
* @static
* @param {Function} [Brood] 将要继承的父类(只继承其原型方法)
* @param {Object} [Proto] 将要扩展的实例方法集
* @param {Object} [ProtoN] 将要扩展的实例方法集
* @return {Function} 类
* @param {function} [Parent] 将要继承的父类(只继承其原型方法)
* @param {object} [properties] 将要扩展的实例方法集
* @param {object} [properties] 将要扩展的实例方法集
* @return {function} 类
*
* @example
* ```
Expand Down Expand Up @@ -131,27 +131,27 @@ Class.superclass = Class.prototype = {
* // tom.school === 'MIT';
* ```
*/
Class.create = function( /*[Brood][, Proto[, ProtoN]]*/ ) {
Class.create = function( /*[Parent][, properties[, ... properties]]*/ ) {

var args = slice.call(arguments, 0),
Dummy,
Brood;
Parent;

Dummy = function() {
this.initialize.apply(this, arguments);
};

if (args[0] && typeof args[0] === 'function') {
Brood = args.shift();
Parent = args.shift();
// 确保继承自 Class 或 Class 的子类
if (!Brood.superclass) {
inherit(Brood, Class);
if (!Parent.superclass) {
inherit(Parent, Class);
}
} else {
Brood = Class;
Parent = Class;
}

inherit(Dummy, Brood);
inherit(Dummy, Parent);

if (args.length) {
args.unshift(Dummy.prototype);
Expand All @@ -163,9 +163,9 @@ Class.create = function( /*[Brood][, Proto[, ProtoN]]*/ ) {
*
* @method extend
* @static
* @param {Object} [Proto] 将要扩展的实例方法集
* @param {Object} [ProtoN] 将要扩展的实例方法集
* @return {Function} 类
* @param {object} [properties] 将要扩展的实例方法集
* @param {object} [properties] 将要扩展的实例方法集
* @return {function} 类
*
* @example
* ```
Expand All @@ -179,7 +179,7 @@ Class.create = function( /*[Brood][, Proto[, ProtoN]]*/ ) {
* });
* ```
*/
Dummy.extend = function( /*[Proto[, ProtoN]]*/ ) {
Dummy.extend = function( /*[properties[, ... properties]]*/ ) {
unshift.call(arguments, Dummy);
return Class.create.apply(null, arguments);
};
Expand Down
32 changes: 29 additions & 3 deletions examples/Class.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,36 @@

---

## Normal usage
## seajs.use

````javascript
```javascript
seajs.use('class', function(Class) {
var Person = Class.create({
initialize: function(name, age) {
this.name = name;
this.age = age;
}
});

var person = new Person('who', '12');

console.log(person.name, person.age);
});
````
```

---

## require

```javascript
// define(function(require, exports, module) {
var Class = require('class');

var Person = module.exports = Class.create({
initialize: function(name, age) {
this.name = name;
this.age = age;
}
});
// });
```
44 changes: 44 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,51 @@
#class

---

[![spm package](http://spmjs.io/badge/class)](http://spmjs.io/package/class)
[![Build Status](https://api.travis-ci.org/crossjs/class.png?branch=master)](http://travis-ci.org/crossjs/class)
[![Coverage Status](https://coveralls.io/repos/crossjs/class/badge.png?branch=master)](https://coveralls.io/r/crossjs/class?branch=master)

> oo, seajs, spm 3
## create `Class.create([Parent][, properties[, ... properties]])`

```javascript
var Person = Class.create({
// 初始化方法,构建实例时自动调用
initialize: function(name, age) {
this.name = name;
this.age = age;
}
});
```

## extend `Child.extend([properties[, ... properties]])`

```javascript
var Student = Person.extend({
// 初始化方法,构建实例时自动调用
initialize: function(name, age, school) {
// 可调用父类的初始化方法
Student.superclass.initialize.apply(this, arguments);
this.school = school;
}
});
```

## extend `instance.extend([properties[, ... properties]])`

```javascript
/* 扩展实例属性 */
var bob = new Person('Bob', 13);
bob.extend({
say: function() {
console.log('My name is ' + this.name + '.');
console.log('I\'m ' + this.age + ' years old.');
}
});
bob.say();
// My name is Bob.
// I'm 13 years old.
```

0 comments on commit 37a13db

Please sign in to comment.