系统如何读取此内容。无法找到背后的逻辑?
var x;
x=10;
x=20;
x=x+x;
document.getElementById('demo').innerHTML=x;
<p id="demo"></p>
答案 0 :(得分:3)
var x, x, x; //declare x 3 times (it makes no sense for me but javascript allows it)
x = 10; // x now is 10
x = 20; // x now is 20
x = x + x; // x now is (x + x) = (20 + 20) = 40
document.getElementById('demo').innerHTML = x; //print 40
答案 1 :(得分:1)
以下是解释的内容:
var x; // excedentary symbol declaration have no effect
x = 10; // value assigned to symbol x
x = 20; // value assigned to symbol x, effectively overwriting previous value
x = x + x; // 20 + 20 = 40
答案 2 :(得分:1)
最后一个值,用于定义我们定义的同名范围内的变量
就像你定义x = 30;
而不是assing 30 to x
之后x value for all is 30
所以不需要创建var x,x,x
var x, x, x;
x = 10;
x = 20;
x = 30;
x = x + x;
document.getElementById('demo').innerHTML = x;
<p id="demo"></p>
答案 3 :(得分:0)
var x;
x = 10; // Here x = 10
x = 30; // Here x = 30
x = x + x; // Now we have 'x=30' from line(2); x=(30 + 30 = 60 is the answer)
document.getElementById('demo').innerHTML=x;
&#13;
<p id="demo"></p>
&#13;