扩展Immutable.Stack返回没有Stack功能的对象

时间:2015-10-06 19:21:53

标签: javascript immutable.js

在尝试扩展Immutable.Stack时,我做错了什么?看起来它不会从Stack父类继承任何东西。

尝试实例化对象时,它将不起作用:console.log(new Buffer([1,2,3]), Immutable.Stack([1,2,3]));

Object { _limit: null } Object { size: 3, _head: Object, __ownerID: undefined, __hash: undefined, __altered: false }

这是我正在使用的实现:

import Immutable from 'immutable';

export default class Buffer extends Immutable.Stack {

constructor(value, limit = null) {
    super(value);
    this._limit = limit;
}

push(/*...values*/) {
    var newSize = this.size + arguments.length;

    if(this._limit >= newSize || this._limit == null) {
        return super.push(arguments);
    }

    var difference = arguments.length - (newSize - this._limit);
    var toSplice = arguments.length - difference;
    return super.push(arguments.splice(0,toSplice>=0 ? toSplice : 0));
}

pushAll(iter) {
    iiter = IndexedIterable(iter);

    var newSize = this.size + iiter.size;
    if(this._limit >= newSize || this._limit == null) {
        return super.pushAll(iter);
    }

    var difference = iiter.size - (newSize - this._limit);
    var toSplice = arguments.length - difference;
    return super.pushAll(iiter.splice(0,toSplice>=0 ? toSplice : 0));
}

unshift(/*...values*/) {
    var newSize = this.size + arguments.length;

    if(this._limit >= newSize || this._limit == null) {
        return super.unshift(arguments);
    }

    var difference = arguments.length - (newSize - this._limit);
    var toSplice = arguments.length - difference;
    return super.unshift(arguments.splice(0,toSplice>=0 ? toSplice : 0));
}

unshiftAll(iter) {
    iiter = IndexedIterable(iter);

    var newSize = this.size + iiter.size;
    if(this._limit >= newSize || this._limit == null) {
        return super.unshiftAll(iter);
    }

    var difference = iiter.size - (newSize - this._limit);
    var toSplice = arguments.length - difference;
    return super.unshiftAll(iiter.splice(0,toSplice>=0 ? toSplice : 0));
}

}

0 个答案:

没有答案