日期:2014-05-16  浏览次数:20321 次

关于闭包
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
(function() {
var object = function() {};
var ooo = {};
ooo.sh = {};
var i = 0;
object.showName = function() {
alert(i);
};
object.prototype.setName = function() {
i = 9999999999999999999999999999999;
};
ooo.sh.showName = function() {
alert(i);
};
window.obj = object;
window.ooo = ooo;
})();
window.obj.setName();
ooo.sh.showName();
</script>
</head>

<body>
</body>
</html>
为什么没运行呢

------解决方案--------------------
 window.obj = object;
改成
window.obj = new object(); 
没有 new 实例它只是个函数,实例后 prototype 属性才会有