var a = {n: 1};
var b = a;
a.x = a = {n: 2};
这里面第三句可以直接拆解为:
a.x = {n : 2};
a = {n : 2};
a.x 赋值的时候,和b还是引用的同一个内存地址,
a赋值的时候,a的内存地址就指向了{n:2},
最后a.x是undefined,b.x是{n:2},
另附一个倒计时js
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="text/html; charset=utf-8" /> <title>无标题文档</title> <!--<script type="text/javascript" src="js/jquery.js"></script>--> </head> <body> <input type="button" id="btn" value="免费获取验证码" οnclick="settime(this)" /> <script type="text/javascript"> var a = { n: 1 } var b = a; a.x = a = { n: 2 } console.log(a.x); console.log(b.x) var countdown = 60; function settime(val) { if (countdown == 0) { val.removeAttribute("disabled"); val.value = "免费获取验证码"; countdown = 60; return; } else { val.setAttribute("disabled", true); val.value = "重新发送(" + countdown + ")"; countdown--; } setTimeout(function () { settime(val) }, 1000) } </script> </body> </html>