log previous

use field to create an object that seems like a primitive that remembers its previous values

by Darby Rathbone

JavaScript

(function () {
    var Var = function (name, v) {
        var r = Object.create((function (val) {

            var field = Object.create({
                toString: function () {
                    return this.current;
                },
                get value() {
                    return {
                        toString: function () {
                            return this.current;
                        },
                        previous: this.previous,
                        current: this.current
                    };

                },
                set value(e) {
                    this.previous = this.value;
                    this.current = e;
                }
            });
            field.value = val;
            field.previous;

            return field;
        })(v||undefined));
        this["_" + name] = r;
        Object.defineProperty(this, name, {
            get: function () {
                return this["_" + name];
            },
            set: function (e) {
                this["_" + name].value = e
            }
        });
        return this[name];

    };
    var displayData = function () {
        [].forEach.call(arguments, function (e) {
            document.body.appendChild(document.createTextNode(e));
            document.body.appendChild(document.createElement('br'));
        });
        // document.body.appendChild(document.createElement('br'));
    };
    var allPrevious = function(e){
        var prev = [];
        e=e.previous;
        while(typeof e.current !=='undefined')
        {              
            //console.log(typeof e.current);
            prev.push(e.current.toString());
            e=e.previous;
        }
        return prev;
    }
    var Field = Var;
    
   Var('c',1);

    Field('b',1);

    
    b = (10);
    b = (100);
    c=1;
    b=c.toString();
    c=2;
    c=4;
    c = 10;
    c = 100;
    c = 1000;
   
    displayData("c="+c ,allPrevious(c));
displayData(allPrevious(b));
    

})()