javascript 构造函数与谷歌浏览器被窃听?
鉴于以下情况script :
var Test = function () {
console.log("constructor");
this.positions = [];
console.log(this.positions);
console.log("constructorEnd");
this.addPosition = function () {
console.log(this.positions);
var i = this.positions.length;
this.positions[i] = "aa";
console.log(this.positions);
}
}
var t = new Test();
console.log("constructed");
t.addPosition();
}
预期结果是:
constructor
[]
constructorEnd
constructed
[]
["aa"]
这是我使用 firebug 可以看到的结果。 chrome(linux,16.0.912.21 dev)上的相同脚本给了我:
constructor
["aa"]
constructorEnd
constructed
["aa"]
["aa"]
这是什么意思? 在 chrome 中添加一个断点来查看发生了什么使其工作(与 firefox 相同的输出)! 这是一个测试用例: http://jsfiddle.net/sNzKq/1/
谢谢
Possible Duplicate:
Bizarre console.log behaviour in Chrome Developer Tools
Given the following script :
var Test = function () {
console.log("constructor");
this.positions = [];
console.log(this.positions);
console.log("constructorEnd");
this.addPosition = function () {
console.log(this.positions);
var i = this.positions.length;
this.positions[i] = "aa";
console.log(this.positions);
}
}
var t = new Test();
console.log("constructed");
t.addPosition();
}
The expected result is :
constructor
[]
constructorEnd
constructed
[]
["aa"]
This is what I can see using firebug.
The very same script on chrome (linux, 16.0.912.21 dev) gives me :
constructor
["aa"]
constructorEnd
constructed
["aa"]
["aa"]
What does that mean ??
Adding a breakpoint in chrome to see what happen make it works (same ouput as firefox) !
Here is a testcase : http://jsfiddle.net/sNzKq/1/
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Chrome 在控制台中显示对象时不会克隆对象,因此它将始终显示当前状态。要记录某个位置的状态,请使用 jQuery 的
$.extend({}, yourObject)
来记录克隆。Chrome does not clone an object when displaying it in the console, so it will always show the current state. To log the state at a certain position, use e.g. jQuery's
$.extend({}, yourObject)
to log a clone.