最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript prototype not working - Stack Overflow

programmeradmin2浏览0评论

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();
发布评论

评论列表(0)

  1. 暂无评论