Skip to content

Commit

Permalink
+ 增加 .is 方法
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudcome committed Feb 5, 2017
1 parent 75f4f87 commit ee490ea
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
32 changes: 32 additions & 0 deletions examples/index.js
Expand Up @@ -7,4 +7,36 @@

'use strict';

var Class = require('../src/index');

var UI = Class.extend({
constructor: function() {
UI.parent(this);
this[_UIOptions] = {width: 100};
},

getWidth: function() {
return this[_UIOptions].width;
}
});
var _UIOptions = UI.sole();

var Window = UI.extend({
constructor: function() {
Window.parent(this);
this[_WindowOptions] = {height: 200};
},

getHeight: function() {
return this[_WindowOptions].height;
}
});
var _WindowOptions = UI.sole();

var win = new Window();
win.getWidth();
// => 100

win.getHeight();
// => 200

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "blear.classes.class",
"version": "1.1.5",
"version": "1.1.6",
"description": "类的继承",
"scripts": {
"live": "browser-sync start --config bs-config.js",
Expand Down
14 changes: 13 additions & 1 deletion src/index.js
Expand Up @@ -187,21 +187,33 @@ Class.prototype = {
className: Class.className,
superClass: Class.superClass
};

/**
* Class 化
* @param constructor {Function} 构造函数
* @returns {*}
*/
Class.ify = function (constructor) {
if (!typeis.Function(constructor)) {
console.log(typeis(constructor));
throw new SyntaxError('ify constructor 必须是构造函数');
}

constructor.extend = makeExtend(constructor);
return constructor;
};

/**
* 判断是否为 Class 扩展类
* @param constructor
* @returns {boolean}
*/
Class.is = function (constructor) {
if (!typeis.Function(constructor)) {
return false;
}

return Boolean(constructor[CLASSIFY_NAME]);
};

/**
* 类的继承
Expand Down
5 changes: 5 additions & 0 deletions test/test.index.js
Expand Up @@ -381,11 +381,16 @@ describe('测试文件', function () {
this.b = b;
}
});
var C = function () {

};

var b = new B(1, 2);

expect(b.length).toEqual(0);
expect(b.b).toEqual(2);
expect(Class.is(B)).toBe(true);
expect(Class.is(C)).toBe(false);
});

it('.ify not function', function () {
Expand Down

0 comments on commit ee490ea

Please sign in to comment.