JS基础学习__像C#一样写JS
1.用JS中的this来模拟C#中的实体类, 将实体整体传出到相应方法中进行操作.
2.
<!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>
<title></title>
<script type="text/javascript">
window.onload = function () {
var v = document.getElementById("Div_Test");
v.onclick = function () { alert("HEHE"); }
var u = new UserInfo("alex", 20, "boy");
v.innerHTML += getUserName(u);
}
//#region 定义一个用户实体类
function UserInfo(name,age,sex) {
this.Name = name;
this.Age = age;
this.Sex = sex;
}
//#endregion
//#region 对用户操作方法
function getUserName(UserInfo) {
if (UserInfo.Sex == "boy")
return "Hello,boy " + UserInfo.Name;
else
return "Hello,girl " + UserInfo.Name;
}
//#endregion
</script>
</head>
<body>
<div id="Div_Test">
This is Test div.
</div>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
window.onload = function () {
var v = document.getElementById("Div_Test");
v.onclick = function () { alert("HEHE"); }
var u = new UserInfo("alex", 20, "boy");
v.innerHTML += getUserName(u);
}
//#region 定义一个用户实体类
function UserInfo(name,age,sex) {
this.Name = name;
this.Age = age;
this.Sex = sex;
}
//#endregion
//#region 对用户操作方法
function getUserName(UserInfo) {
if (UserInfo.Sex == "boy")
return "Hello,boy " + UserInfo.Name;
else
return "Hello,girl " + UserInfo.Name;
}
//#endregion
</script>
</head>
<body>
<div id="Div_Test">
This is Test div.
</div>
</body>
</html>