Search code examples
prototypejavascriptfunction-prototypes

javascript prototype not working


Am I mistaking what .prototype is supposed to do, or is this just not working??

window.dump = function () {
    for (var i = 0, x = dump.list.length; i < x; ++i) console.log.apply(this, dump.list[i]);
    if (arguments.length && typeof arguments[0] === 'boolean' && arguments[0]) dump.purge();
}
dump.prototype = {
    list : [],
    log : function () {
        dump.list.push(arguments);
    },
    purge : function () {
        dump.list = [];
    }
}
dump.log('test1');
dump.log('test2');
dump();

I expect "test1" and "test2" to be passed through console.log, instead dump.log is not defined. However dump.prototype.log is.

edit: I've tried the following, and I just can't seem to get this prototype thing right.

window.dump = new function () {
    this.list = [];
    this.log = function () {
        this.list.push(arguments);
    }
    this.purge = function () {
        return this.list = [];
    }
    return function () {
        for (var i = 0, x = this.list.length; i < x; ++i) console.log.apply(this, this.list[i]);
        if (arguments.length && typeof arguments[0] === 'boolean' && arguments[0]) this.purge();
    }
}

I guess what I'm asking is, what is the correct way to be able to use my code as follows?

dump.log('test1');
dump.log('test2');
dump();

edit: Here's a final result thanks to Matthew Flaschen, for anyone who's interested in building from it.

(function () {
    var console_log = Function.prototype.bind.call(console.log, console);
    window.dump = function () {
        for (var i = 0, x = dump.list.length; i < x; ++i) console_log.apply(this, dump.list[i]);
        if (arguments.length && typeof arguments[0] === 'boolean' && arguments[0]) dump.purge();
    };
    dump.list = [];
    dump.log = function () {
        dump.list.push(arguments);
    }
    dump.purge = function () {
        dump.list = [];
    }
})();

I've had to assign console_log to wrap console.log, because apparently console is not a standard object. Therefore it is not a standard Function object with the apply method. Proof that I do actually use Google.


Solution

  • Yes, a correct version would be the below. dumper is a constructor function. Thus, it initializes the list member.

    Below, we set the dumper prototype with the desired methods. These can also use this. Any instance of dumper (such as d) will have these methods.

    window.dumper = function () {
        this.list = [];
    };
    
    dumper.prototype = {
    
        log : function () {
            this.list.push(arguments);
        },
        purge : function () {
            this.list = [];
        },
        dump : function () {
            for (var i = 0, x = this.list.length; i < x; ++i) console.log.apply(this, this.list[i]);
            if (arguments.length && typeof arguments[0] === 'boolean' && arguments[0]) this.purge();
        }
    }
    
    
    var d = new dumper();        
    
    d.log('test1');
    d.log('test2');
    d.dump();