【发布时间】:2013-08-14 11:44:02
【问题描述】:
我使用火狐。
此代码记录[]。
var log = console.log;
function new_comb(aComb) {
var res = [];
log(aComb); // <- This is the line
for (var p in aComb) {
var peg = aComb[p];
var current = peg[peg.length - 1];
for (var i = 0; i < aComb.length; i++) {
if (i == p) continue;
if (current > aComb[i][aComb[i].length - 1]) continue;
var tmp = aComb.splice(0);
tmp[i].push(current);
tmp[p].pop();
res.push(tmp);
}
}
return res;
}
var comb = [
[3, 1],
[9, 2],
[15, 0]];
var res = new_comb(comb);
此代码记录了正确的值。
var log = console.log;
function new_comb(aComb) {
var res = [];
log(aComb); // <- This is the line
// note that I comment this out.
/*for (var p in aComb) {
var peg = aComb[p];
var current = peg[peg.length - 1];
for (var i = 0; i < aComb.length; i++) {
if (i == p) continue;
if (current > aComb[i][aComb[i].length - 1]) continue;
var tmp = aComb.splice(0);
tmp[i].push(current);
tmp[p].pop();
res.push(tmp);
}
}*/
return res;
}
var comb = [
[3, 1],
[9, 2],
[15, 0]];
var res = new_comb(comb);
为什么会这样?
【问题讨论】:
-
不是你的问题,但你应该更喜欢
var log = console.log.bind(console);之类的东西来保持上下文 -
或者更好的是,使用
console.log或任何其他主机提供的功能:function log(msg) { return console.log(msg); }主机提供的功能确实可以(并且被允许)非常松散。不能保证它们有bind,并且不能保证它们在通过其他引用而不是它们的规范引用时正常工作。 -
@T.J.Crowder,什么是主机提供的功能?
-
@learner:您在the JavaScript specification 中看不到的任何内容。所以
console的各种函数,alert,prompt,confirm,所有的DOM函数,...
标签: javascript firefox console.log