admin管理员组文章数量:1024638
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.
1 Answer
Reset to default 6Yes, 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();
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.
1 Answer
Reset to default 6Yes, 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();
本文标签: javascript prototype not workingStack Overflow
版权声明:本文标题:javascript prototype not working - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745600010a2158400.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论