|
// example-of-function-based-mixin-composition-with-class-and-shared-local-state.js |
|
|
|
function withExposableSharedLocalState (state) { |
|
state = ((typeof state === "object") && state) || (void 0); |
|
|
|
var json_stringify = JSON.stringify; |
|
var json_parse = JSON.parse; |
|
|
|
var compositeType = this; |
|
|
|
compositeType.valueOf = function () { |
|
return ((typeof state !== "undefined") ? json_parse(json_stringify(state)) : state); |
|
}; |
|
compositeType.toString = function () { |
|
return json_stringify(state); |
|
}; |
|
return compositeType; |
|
} |
|
|
|
const withEnumerableFirstLast = (function withEnumerableFirstLastFactory () { |
|
function first () { |
|
return this[0]; |
|
} |
|
function last () { |
|
return this[this.length - 1]; |
|
} |
|
return function withEnumerableFirstLast () { |
|
const enumerableType = this; |
|
|
|
enumerableType.first = first; |
|
enumerableType.last = last; |
|
|
|
return enumerableType; |
|
}; |
|
}()); |
|
|
|
function withEnumerableListProxy (sharedList) { |
|
sharedList = withEnumerableFirstLast.call((Array.isArray(sharedList) && sharedList) || []); |
|
|
|
const compositeType = this; |
|
|
|
compositeType.first = function () { |
|
return sharedList.first(); |
|
}; |
|
compositeType.last = function () { |
|
return sharedList.last(); |
|
}; |
|
return compositeType; |
|
} |
|
|
|
class Queue { |
|
constructor () { |
|
const |
|
queue = this, |
|
list = []; |
|
|
|
queue.enqueue = function (item) { |
|
list.push(item); |
|
return item; |
|
}; |
|
queue.dequeue = function () { |
|
return list.shift(); |
|
}; |
|
withEnumerableListProxy.call(queue, list); |
|
withExposableSharedLocalState.call(queue, list); |
|
|
|
return queue; |
|
} |
|
} |
|
|
|
const queue = new Queue(); |
|
|
|
queue.enqueue("the"); |
|
queue.enqueue("quick"); |
|
queue.enqueue("brown"); |
|
|
|
console.log("queue : ", queue); |
|
|
|
console.log("queue.enqueue('fox') : ", queue.enqueue('fox')); |
|
console.log("queue.first() : ", queue.first()); |
|
console.log("queue.last() : ", queue.last()); |
|
|
|
console.log("queue.dequeue() : ", queue.dequeue()); |
|
console.log("queue.first() : ", queue.first()); |
|
console.log("queue.last() : ", queue.last()); |
|
|
|
console.log("queue.valueOf() : ", queue.valueOf()); |
|
console.log("queue.toString() : ", queue.toString()); |
|
console.log("('' + queue) : ", ('' + queue)); |