日期:2014-05-16 浏览次数:20621 次
var Lib = {};
Lib.Do = {};
...
function Person(name,age,sex) {
this.name = name || "jee";
this.age = age || 24;
this.sex = sex || true
}
var p = new Person("jee",24,true);
alert(p.getName());
alert(p.name);
function person(name,age,sex) {
var _name = name || "jee";
var _age = age || 24;
var _sex = sex || true;
return {
getname : function() {
return _name;
},
setname : function(n) {
_name = n;
}
getage : function() {
return _age
}
...
};
}
<!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 runat="server">
<title></title>
<script type="text/javascript">
window.onload = function() {
var li = document.getElementsByTagName("li");
for (var i = 0; i < li.length; i++) {
li[i].onclick = function() {
alert(i);
};
}
};
</script>
<style>
li {
width:100px;
background-color:red;
cursor:pointer;
line-height:20px;
margin-bottom:5px;
}
</style>
</head>
<body>
<ul>
<li></li>
<li></li>
<li></li>
</ul>
</body>
</html>
window.onload = function() {
var li = document.getElementsByTagName("li");
for (var i = 0; i < li.length; i++) {
li[i].onclick = function(i) {
return function() {
alert(i);
};
}(i);//i作为参数传递给这个自执行函数,在绑定onclick的时候,该函数已执行,而这个函数体内部调用的就是该i的值了
}
};