mirror of
https://github.com/flynx/object.js.git
synced 2025-10-29 02:20:08 +00:00
made things a bit more obvious: <mixin>.data -> <mixin>.proto
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
bed2eed2de
commit
b77550f97f
40
object.js
40
object.js
@ -1208,11 +1208,11 @@ function(base, ...objects){
|
|||||||
// Mixin wrapper/object...
|
// Mixin wrapper/object...
|
||||||
//
|
//
|
||||||
// Create a new mixin...
|
// Create a new mixin...
|
||||||
// Mixin(name, data, ..)
|
// Mixin(name, proto, ..)
|
||||||
// -> mixin
|
// -> mixin
|
||||||
//
|
//
|
||||||
// Create a new mixin setting the default mode...
|
// Create a new mixin setting the default mode...
|
||||||
// Mixin(name, mode, data, ..)
|
// Mixin(name, mode, proto, ..)
|
||||||
// -> mixin
|
// -> mixin
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
@ -1242,7 +1242,7 @@ function(base, ...objects){
|
|||||||
// BasicMixin(o)
|
// BasicMixin(o)
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
// NOTE: the constructor will allways create a new .data object but will
|
// NOTE: the constructor will allways create a new .proto object but will
|
||||||
// not do a deep copy into it -- see mixin(..) / mixinFlat(..)
|
// not do a deep copy into it -- see mixin(..) / mixinFlat(..)
|
||||||
//
|
//
|
||||||
// XXX should this move to its own modue???
|
// XXX should this move to its own modue???
|
||||||
@ -1262,38 +1262,38 @@ Constructor('Mixin', {
|
|||||||
}, {
|
}, {
|
||||||
name: null,
|
name: null,
|
||||||
|
|
||||||
// mixin data...
|
// mixin proto...
|
||||||
data: null,
|
proto: null,
|
||||||
|
|
||||||
// data "copy" mode...
|
// proto "copy" mode...
|
||||||
//
|
//
|
||||||
// This can be:
|
// This can be:
|
||||||
// 'proto' - mix data into prototype chain (default)
|
// 'proto' - mix proto into prototype chain (default)
|
||||||
// 'flat' - use mixinFlat(..) to copy data
|
// 'flat' - use mixinFlat(..) to copy proto
|
||||||
// 'soft' - like 'flat' but uses mixinFlat('soft', ..)
|
// 'soft' - like 'flat' but uses mixinFlat('soft', ..)
|
||||||
mode: 'proto',
|
mode: 'proto',
|
||||||
|
|
||||||
// base API...
|
// base API...
|
||||||
//
|
//
|
||||||
isMixed: function(target){
|
isMixed: function(target){
|
||||||
return this.constructor.hasMixin(target, this.data) },
|
return this.constructor.hasMixin(target, this.proto) },
|
||||||
mixout: function(target){
|
mixout: function(target){
|
||||||
return this.constructor.mixout(target, this.data) },
|
return this.constructor.mixout(target, this.proto) },
|
||||||
|
|
||||||
// mix into target...
|
// mix into target...
|
||||||
__call__: function(_, target, mode=this.mode){
|
__call__: function(_, target, mode=this.mode){
|
||||||
typeof(target) == typeof('str')
|
typeof(target) == typeof('str')
|
||||||
&& ([_, mode, target] = arguments)
|
&& ([_, mode, target] = arguments)
|
||||||
return mode == 'flat' ?
|
return mode == 'flat' ?
|
||||||
this.constructor.mixinFlat(target, this.data)
|
this.constructor.mixinFlat(target, this.proto)
|
||||||
: mode == 'soft' ?
|
: mode == 'soft' ?
|
||||||
this.constructor.mixinFlat('soft', target, this.data)
|
this.constructor.mixinFlat('soft', target, this.proto)
|
||||||
: this.constructor.mixin(target, this.data) },
|
: this.constructor.mixin(target, this.proto) },
|
||||||
|
|
||||||
__init__: function(name, ...data){
|
__init__: function(name, ...proto){
|
||||||
// Mixin(name, mode, ...) -- handle default mode...
|
// Mixin(name, mode, ...) -- handle default mode...
|
||||||
typeof(data[0]) == typeof('str')
|
typeof(proto[0]) == typeof('str')
|
||||||
&& (this.mode = data.shift())
|
&& (this.mode = proto.shift())
|
||||||
// name...
|
// name...
|
||||||
// NOTE: .defineProperty(..) is used because this is a function
|
// NOTE: .defineProperty(..) is used because this is a function
|
||||||
// and function's .name is not too configurable...
|
// and function's .name is not too configurable...
|
||||||
@ -1303,12 +1303,12 @@ Constructor('Mixin', {
|
|||||||
// XXX is this effective???
|
// XXX is this effective???
|
||||||
// ...will this show up in DevTools???
|
// ...will this show up in DevTools???
|
||||||
Object.defineProperty(this, 'name', { value: name })
|
Object.defineProperty(this, 'name', { value: name })
|
||||||
// create/merge .data...
|
// create/merge .proto...
|
||||||
this.data = this.constructor.mixinFlat({},
|
this.proto = this.constructor.mixinFlat({},
|
||||||
...data.map(function(e){
|
...proto.map(function(e){
|
||||||
// handle bare objects and mixins differently...
|
// handle bare objects and mixins differently...
|
||||||
return e instanceof Mixin ?
|
return e instanceof Mixin ?
|
||||||
e.data
|
e.proto
|
||||||
: e })) },
|
: e })) },
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "ig-object",
|
"name": "ig-object",
|
||||||
"version": "6.1.4",
|
"version": "6.2.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "object.js",
|
"main": "object.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
10
test.js
10
test.js
@ -737,13 +737,13 @@ var cases = test.Cases({
|
|||||||
var A = assert(object.Mixin('A', {
|
var A = assert(object.Mixin('A', {
|
||||||
a: 'aaa',
|
a: 'aaa',
|
||||||
}), 'Mixin("A", ...)')
|
}), 'Mixin("A", ...)')
|
||||||
assert(A.data.a == 'aaa', 'A content')
|
assert(A.proto.a == 'aaa', 'A content')
|
||||||
assert(A.mode == 'proto', 'A mode')
|
assert(A.mode == 'proto', 'A mode')
|
||||||
|
|
||||||
var B = assert(object.Mixin('B', 'flat', {
|
var B = assert(object.Mixin('B', 'flat', {
|
||||||
b: 'bbb'
|
b: 'bbb'
|
||||||
}), 'Mixin("B", "flat", ..)')
|
}), 'Mixin("B", "flat", ..)')
|
||||||
assert(B.data.b == 'bbb', 'B content')
|
assert(B.proto.b == 'bbb', 'B content')
|
||||||
assert(B.mode == 'flat', 'B mode')
|
assert(B.mode == 'flat', 'B mode')
|
||||||
|
|
||||||
var C = assert(object.Mixin('C',
|
var C = assert(object.Mixin('C',
|
||||||
@ -753,9 +753,9 @@ var cases = test.Cases({
|
|||||||
get c(){
|
get c(){
|
||||||
return 'ccc' },
|
return 'ccc' },
|
||||||
}), 'Mixin("C", A, B, ...)')
|
}), 'Mixin("C", A, B, ...)')
|
||||||
assert(C.data.a == 'aaa', 'C content from A')
|
assert(C.proto.a == 'aaa', 'C content from A')
|
||||||
assert(C.data.b == 'bbb', 'C content from B')
|
assert(C.proto.b == 'bbb', 'C content from B')
|
||||||
assert(C.data.c == 'ccc', 'C content local')
|
assert(C.proto.c == 'ccc', 'C content local')
|
||||||
|
|
||||||
var x = assert(C({}), 'C({})')
|
var x = assert(C({}), 'C({})')
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user