2 module.exports = ProtoList
4 function setProto(obj, proto) {
5 if (typeof Object.setPrototypeOf === "function")
6 return Object.setPrototypeOf(obj, proto)
11 function ProtoList () {
14 Object.defineProperty(this, 'root', {
15 get: function () { return root },
18 if (this.list.length) {
19 setProto(this.list[this.list.length - 1], r)
28 { get length () { return this.list.length }
31 for (var i in this.list[0]) k.push(i)
36 this.keys.forEach(function (k) { o[k] = this.get(k) }, this)
42 , push : function (obj) {
43 if (typeof obj !== "object") obj = {valueOf:obj}
44 if (this.list.length >= 1) {
45 setProto(this.list[this.list.length - 1], obj)
47 setProto(obj, this.root)
48 return this.list.push(obj)
51 if (this.list.length >= 2) {
52 setProto(this.list[this.list.length - 2], this.root)
54 return this.list.pop()
56 , unshift : function (obj) {
57 setProto(obj, this.list[0] || this.root)
58 return this.list.unshift(obj)
60 , shift : function () {
61 if (this.list.length === 1) {
62 setProto(this.list[0], this.root)
64 return this.list.shift()
66 , get : function (key) {
67 return this.list[0][key]
69 , set : function (key, val, save) {
70 if (!this.length) this.push({})
71 if (save && this.list[0].hasOwnProperty(key)) this.push({})
72 return this.list[0][key] = val
74 , forEach : function (fn, thisp) {
75 for (var key in this.list[0]) fn.call(thisp, key, this.list[0][key])
77 , slice : function () {
78 return this.list.slice.apply(this.list, arguments)
80 , splice : function () {
82 var ret = this.list.splice.apply(this.list, arguments)
83 for (var i = 0, l = this.list.length; i < l; i++) {
84 setProto(this.list[i], this.list[i + 1] || this.root)