From e9ad7e9271cc625ad5afe730e46b4f1371a1f6ec Mon Sep 17 00:00:00 2001 From: dengxinxin Date: Thu, 28 Aug 2014 00:35:41 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E8=AE=BF=E9=97=AE=E5=99=A8=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/defineAccessor.js | 29 +++++++++++++++++++++++++++++ src/main.js | 13 +++++++++++++ src/oo.js | 15 +-------------- 3 files changed, 43 insertions(+), 14 deletions(-) create mode 100644 src/defineAccessor.js create mode 100644 src/main.js diff --git a/src/defineAccessor.js b/src/defineAccessor.js new file mode 100644 index 0000000..d5aaec6 --- /dev/null +++ b/src/defineAccessor.js @@ -0,0 +1,29 @@ +/** + * Created by exodia on 14-8-28. + */ +void function (define, undefined) { + + define( + function (require) { + var ATTRIBUTES = '__eooAttributes__'; + + function simpleGetter() { + return typeof this[ATTRIBUTES] === 'object' ? this[ATTRIBUTES][name] : undefined; + } + + function simpleSetter(value) { + this[ATTRIBUTES] = this[ATTRIBUTES] || {}; + this[ATTRIBUTES][name] = value; + } + + return function (obj, name, accessor) { + var upperName = name.charAt(0).toUpperCase() + name.slice(1); + var getter = 'get' + upperName; + var setter = 'set' + upperName; + obj[getter] = !accessor || typeof accessor.get !== 'function' ? simpleGetter : accessor.get; + obj[setter] = !accessor || typeof accessor.set !== 'function' ? simpleSetter : accessor.set; + }; + } + ); + +}(typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }); \ No newline at end of file diff --git a/src/main.js b/src/main.js new file mode 100644 index 0000000..d2dfdd7 --- /dev/null +++ b/src/main.js @@ -0,0 +1,13 @@ +/** + * Created by exodia on 14-8-28. + */ +void function (define) { + define( + function (require) { + var oo = require('./oo'); + oo.defineAccessor = require('./defineAccessor'); + + return oo; + } + ); +}(typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }); \ No newline at end of file diff --git a/src/oo.js b/src/oo.js index 5674d8b..7e09799 100644 --- a/src/oo.js +++ b/src/oo.js @@ -173,21 +173,8 @@ void function (define) { */ function inherit(BaseClass) { var kclass = function () { - /** - * 若未进行 constructor 的重写,则klass.prototype.constructor指向BaseClass.prototype.constructor - */ + // 若未进行 constructor 的重写,则klass.prototype.constructor指向BaseClass.prototype.constructor return kclass.prototype.constructor.apply(this, arguments); - - // 下面是之前的代码,我也不知道为什么写的这么多,还有 bug- -!!5555~~ - - // 兼容非 oo 创建的类继承 oo 类时的构造函数调用 - /*if (kclass.caller === this.constructor) { - return kclass.prototype.constructor.apply(this, arguments); - } - - return this.constructor !== kclass && - typeof this.constructor == 'function' && - this.constructor.apply(this, arguments);*/ }; Empty.prototype = BaseClass.prototype; From ebb7fc3f5660c9da9487842cd0ba2be9d7c05ca6 Mon Sep 17 00:00:00 2001 From: dengxinxin Date: Thu, 28 Aug 2014 00:41:06 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E7=BB=99=20eoo=20=E7=9A=84=E9=9A=90?= =?UTF-8?q?=E8=97=8F=E5=B1=9E=E6=80=A7=E5=90=8D=E7=A7=B0=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=89=8D=E7=BC=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/oo.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/oo.js b/src/oo.js index 7e09799..b67d614 100644 --- a/src/oo.js +++ b/src/oo.js @@ -3,9 +3,9 @@ // - demo void function (define) { define(function () { - var Empty = function () {}; - var NAME_PROPERTY_NAME = '__name__'; - var OWNER_PROPERTY_NAME = '__owner__'; + var Empty = function () { }; + var NAME_PROPERTY_NAME = '__eooName__'; + var OWNER_PROPERTY_NAME = '__eooOwner__'; /** * 简单的 js oo 库 From 5ef00ce43975c20cd56593189375d91bc77c06ed Mon Sep 17 00:00:00 2001 From: dengxinxin Date: Thu, 28 Aug 2014 12:03:34 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E5=8A=A0=E4=BA=9B=E6=B3=A8=E9=87=8A?= =?UTF-8?q?=EF=BC=8C=E8=87=AA=E5=AE=9A=E4=B9=89accessor=20=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E4=B8=8D=E9=BB=98=E8=AE=A4=E7=94=9F=E6=88=90=20access?= =?UTF-8?q?or?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo/index.html | 10 +++++++++- src/defineAccessor.js | 20 ++++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/demo/index.html b/demo/index.html index 4ac9fc3..e8a2459 100644 --- a/demo/index.html +++ b/demo/index.html @@ -15,7 +15,7 @@ baseUrl: '../src' }); -require(['oo'], function (Class) { +require(['main'], function (Class) { var Super = Class({ superProp1: 'superProp1', superProp2: 'superProp2', @@ -240,6 +240,14 @@ var e2 = new EooClass2('prop1', 'prop2'); e2.test(); + + Class.defineAccessor(Sub.prototype, 'testProp'); + var accessorSub = new Sub(); + accessorSub.setTestProp('test'); + printLine(accessorSub.getTestProp()); + accessorSub.setTestProp('test1'); + printLine(accessorSub.getTestProp()); + }); diff --git a/src/defineAccessor.js b/src/defineAccessor.js index d5aaec6..a57ed8e 100644 --- a/src/defineAccessor.js +++ b/src/defineAccessor.js @@ -16,12 +16,28 @@ void function (define, undefined) { this[ATTRIBUTES][name] = value; } + /** + * 根据指定的属性名生成对应的accessor + * + * @param {Object | Function} obj 需要生成 accessor 的对象 + * @param {string} name 需要生成 accessor 的属性名称 + * @param {Object | Function} [accessor] 自定义的 getter 和 setter 配置 + * @param {Function} [accessor.get] 自定义的 getter 函数, 配置了accessor,但未设置 get,则不会生成 getter + * @param {Function} [accessor.set] 自定义的 setter 函数, 配置了accessor,但未设置 set,则不会生成 setter + */ return function (obj, name, accessor) { var upperName = name.charAt(0).toUpperCase() + name.slice(1); var getter = 'get' + upperName; var setter = 'set' + upperName; - obj[getter] = !accessor || typeof accessor.get !== 'function' ? simpleGetter : accessor.get; - obj[setter] = !accessor || typeof accessor.set !== 'function' ? simpleSetter : accessor.set; + + if (!accessor) { + obj[getter] = !accessor || typeof accessor.get !== 'function' ? simpleGetter : accessor.get; + obj[setter] = !accessor || typeof accessor.set !== 'function' ? simpleSetter : accessor.set; + } + else { + typeof accessor.get === 'function' && (obj[getter] = accessor.get); + typeof accessor.set === 'function' && (obj[setter] = accessor.set); + } }; } ); From caddee49234499da8acfa1138ba9d30d2cc56a15 Mon Sep 17 00:00:00 2001 From: dengxinxin Date: Thu, 28 Aug 2014 16:34:05 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E6=94=B9=E4=B8=AA=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/defineAccessor.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/defineAccessor.js b/src/defineAccessor.js index a57ed8e..cbe8a65 100644 --- a/src/defineAccessor.js +++ b/src/defineAccessor.js @@ -5,15 +5,15 @@ void function (define, undefined) { define( function (require) { - var ATTRIBUTES = '__eooAttributes__'; + var MEMBERS = '__eooPrivateMembers__'; function simpleGetter() { - return typeof this[ATTRIBUTES] === 'object' ? this[ATTRIBUTES][name] : undefined; + return typeof this[MEMBERS] === 'object' ? this[MEMBERS][name] : undefined; } function simpleSetter(value) { - this[ATTRIBUTES] = this[ATTRIBUTES] || {}; - this[ATTRIBUTES][name] = value; + this[MEMBERS] = this[MEMBERS] || {}; + this[MEMBERS][name] = value; } /** From d56da3f2ffbd14429e0892c602de60908ba77897 Mon Sep 17 00:00:00 2001 From: dengxinxin Date: Thu, 28 Aug 2014 16:53:15 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E4=BD=BF=E7=94=A8=20new=20Function=20?= =?UTF-8?q?=E7=94=9F=E6=88=90=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo/index.html | 5 ++++- src/defineAccessor.js | 17 ++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/demo/index.html b/demo/index.html index e8a2459..144a5f6 100644 --- a/demo/index.html +++ b/demo/index.html @@ -242,7 +242,10 @@ e2.test(); Class.defineAccessor(Sub.prototype, 'testProp'); - var accessorSub = new Sub(); + window.accessorSub = new Sub(); + console.log(accessorSub.setTestProp); + console.log(accessorSub.getTestProp); + accessorSub.setTestProp('test'); printLine(accessorSub.getTestProp()); accessorSub.setTestProp('test1'); diff --git a/src/defineAccessor.js b/src/defineAccessor.js index cbe8a65..7a617f2 100644 --- a/src/defineAccessor.js +++ b/src/defineAccessor.js @@ -7,13 +7,16 @@ void function (define, undefined) { function (require) { var MEMBERS = '__eooPrivateMembers__'; - function simpleGetter() { - return typeof this[MEMBERS] === 'object' ? this[MEMBERS][name] : undefined; + function simpleGetter(name) { + var body = 'return typeof this.' + MEMBERS + ' === \'object\' ? this.' + + MEMBERS + '.' + name + ' : undefined;'; + return new Function(body); } - function simpleSetter(value) { - this[MEMBERS] = this[MEMBERS] || {}; - this[MEMBERS][name] = value; + function simpleSetter(name) { + var body = 'this.' + MEMBERS + ' = this.' + MEMBERS + ' || {};\n' + + 'this.' + MEMBERS + '.' + name + ' = value;' ; + return new Function('value', body); } /** @@ -31,8 +34,8 @@ void function (define, undefined) { var setter = 'set' + upperName; if (!accessor) { - obj[getter] = !accessor || typeof accessor.get !== 'function' ? simpleGetter : accessor.get; - obj[setter] = !accessor || typeof accessor.set !== 'function' ? simpleSetter : accessor.set; + obj[getter] = !accessor || typeof accessor.get !== 'function' ? simpleGetter(name) : accessor.get; + obj[setter] = !accessor || typeof accessor.set !== 'function' ? simpleSetter(name) : accessor.set; } else { typeof accessor.get === 'function' && (obj[getter] = accessor.get); From 1cec8c9a9884ed1869aad5a53e2e64f5316e8b86 Mon Sep 17 00:00:00 2001 From: dengxinxin Date: Thu, 28 Aug 2014 17:03:57 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E6=94=B9=E5=8F=98=20name=20=E7=9A=84?= =?UTF-8?q?=E8=AE=BF=E9=97=AE=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/defineAccessor.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/defineAccessor.js b/src/defineAccessor.js index 7a617f2..83f6698 100644 --- a/src/defineAccessor.js +++ b/src/defineAccessor.js @@ -9,13 +9,13 @@ void function (define, undefined) { function simpleGetter(name) { var body = 'return typeof this.' + MEMBERS + ' === \'object\' ? this.' - + MEMBERS + '.' + name + ' : undefined;'; + + MEMBERS + '[\'' + name + '\'] : undefined;'; return new Function(body); } function simpleSetter(name) { var body = 'this.' + MEMBERS + ' = this.' + MEMBERS + ' || {};\n' - + 'this.' + MEMBERS + '.' + name + ' = value;' ; + + 'this.' + MEMBERS + '[\'' + name + '\'] = value;' ; return new Function('value', body); }