最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript prototype not working - Stack Overflow

matteradmin4PV0评论

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();
Post a comment

comment list (0)

  1. No comments so far