admin管理员组文章数量:1026373
//1st question
var x = 4,
obj = {
x: 3,
bar: function() {
var x = 2;
setTimeout(function() {
var x = 1;
alert(this.x);
}, 1000);
}
};
obj.bar();
//2nd question
function foo(a) {
arguments[0] = 2;
alert(a);
}
foo(1);
1.why it returns 4 instead of 1? i thought this.x refer to 1, but it seems wrong....i just dont understand why it returns 4
2.why it return alert 2 instead of 1, i thought i pass a to function a, and as far as i know, i pass 1 to function foo, and 1 should be alerted because of a(which is 1 when i pass)....i just don't understand why it alert 2
//1st question
var x = 4,
obj = {
x: 3,
bar: function() {
var x = 2;
setTimeout(function() {
var x = 1;
alert(this.x);
}, 1000);
}
};
obj.bar();
//2nd question
function foo(a) {
arguments[0] = 2;
alert(a);
}
foo(1);
1.why it returns 4 instead of 1? i thought this.x refer to 1, but it seems wrong....i just dont understand why it returns 4
2.why it return alert 2 instead of 1, i thought i pass a to function a, and as far as i know, i pass 1 to function foo, and 1 should be alerted because of a(which is 1 when i pass)....i just don't understand why it alert 2
Share Improve this question edited May 2, 2016 at 19:02 Mike Cluck 32.5k13 gold badges83 silver badges94 bronze badges asked May 2, 2016 at 19:02 Kevin MogiKevin Mogi 1171 silver badge7 bronze badges 1-
console.log
this
and you will see that it is referring to the window. on that level x has been defined as 4. This is all about scoping. check out this article, especially the part aboutthis
inside of closures ;) javascriptissexy./… – HolyMoly Commented May 2, 2016 at 19:37
2 Answers
Reset to default 5The runtime (in non-strict mode) invokes the
setTimeout()
callback withthis
bound towindow
(the global context), sothis.x
refers to the outerx
.The
arguments
object serves as a way to alias the formal parameters of a function. Setting the value ofarguments[0]
also sets the value of the first declared parameter to the function.
1. why it returns 4 instead of 1?
Notice the first initialization: var x = 4
, which in non-strict mode attaches a property x
to global object: window.x = 4
.
setTimeout(function() {
var x = 1;
alert(this.x);
}, 1000);
setTimout()
callback has this
context as the global object. And actually calling alert(this.x)
-> alert(window.x)
-> alert(4)
.
2.why it return alert 2 instead of 1
arguments
object represents the list of function arguments. When modifying it, you actually modify the arguments values: arguments[0] = 2
modifies first argument a = 2
.
//1st question
var x = 4,
obj = {
x: 3,
bar: function() {
var x = 2;
setTimeout(function() {
var x = 1;
alert(this.x);
}, 1000);
}
};
obj.bar();
//2nd question
function foo(a) {
arguments[0] = 2;
alert(a);
}
foo(1);
1.why it returns 4 instead of 1? i thought this.x refer to 1, but it seems wrong....i just dont understand why it returns 4
2.why it return alert 2 instead of 1, i thought i pass a to function a, and as far as i know, i pass 1 to function foo, and 1 should be alerted because of a(which is 1 when i pass)....i just don't understand why it alert 2
//1st question
var x = 4,
obj = {
x: 3,
bar: function() {
var x = 2;
setTimeout(function() {
var x = 1;
alert(this.x);
}, 1000);
}
};
obj.bar();
//2nd question
function foo(a) {
arguments[0] = 2;
alert(a);
}
foo(1);
1.why it returns 4 instead of 1? i thought this.x refer to 1, but it seems wrong....i just dont understand why it returns 4
2.why it return alert 2 instead of 1, i thought i pass a to function a, and as far as i know, i pass 1 to function foo, and 1 should be alerted because of a(which is 1 when i pass)....i just don't understand why it alert 2
Share Improve this question edited May 2, 2016 at 19:02 Mike Cluck 32.5k13 gold badges83 silver badges94 bronze badges asked May 2, 2016 at 19:02 Kevin MogiKevin Mogi 1171 silver badge7 bronze badges 1-
console.log
this
and you will see that it is referring to the window. on that level x has been defined as 4. This is all about scoping. check out this article, especially the part aboutthis
inside of closures ;) javascriptissexy./… – HolyMoly Commented May 2, 2016 at 19:37
2 Answers
Reset to default 5The runtime (in non-strict mode) invokes the
setTimeout()
callback withthis
bound towindow
(the global context), sothis.x
refers to the outerx
.The
arguments
object serves as a way to alias the formal parameters of a function. Setting the value ofarguments[0]
also sets the value of the first declared parameter to the function.
1. why it returns 4 instead of 1?
Notice the first initialization: var x = 4
, which in non-strict mode attaches a property x
to global object: window.x = 4
.
setTimeout(function() {
var x = 1;
alert(this.x);
}, 1000);
setTimout()
callback has this
context as the global object. And actually calling alert(this.x)
-> alert(window.x)
-> alert(4)
.
2.why it return alert 2 instead of 1
arguments
object represents the list of function arguments. When modifying it, you actually modify the arguments values: arguments[0] = 2
modifies first argument a = 2
.
本文标签: Super Tricky Javascript quizneed to figure out about the answerStack Overflow
版权声明:本文标题:Super Tricky Javascript quiz, need to figure out about the answer - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745643546a2160889.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论