javascript prototype not working - Stack Overflow

Am I mistaking what .prototype is supposed to do, or is this just not working??window.dump = function (

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.

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.

Share Improve this question edited Feb 18, 2012 at 7:15 Shea asked Feb 18, 2012 at 6:19 SheaShea 1,9502 gold badges18 silver badges42 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 6

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();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745600011a4635345.html

相关推荐

  • javascript prototype not working - Stack Overflow

    Am I mistaking what .prototype is supposed to do, or is this just not working??window.dump = function (

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信