JS(面向对象--模拟类、对象)
<!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=gb2312" />
<title>博客日志</title>
</head>
<body>
<script>
/*模拟一个类*/
function Person(){
var name="lisi";
}
//往该类中添加公共的方法
Person.prototype.getName=function(){
alert("method run....");
}
//创建对象
var p=new Person();
p.getName();
</script>
</body>
</html>
JS(面向对象--模拟完整的类)
<!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=gb2312" />
<title>博客日志</title>
</head>
<body>
<script>
function Person(){
//私有的静态属性
var sex="男";
//私有的静态方法
function checked(sex){
if(sex=="男"){
alert("ok...");
}
else{
alert("no no ");
}
}
//私有属性
this.name="lisi";
//私有方法
this.checkSex(sex){
checked(sex);//调用私有的静态方法
}
}
//公有静态方法
Person.show=function(){
alert("show run...");
}
//公有方法
Person.prototype.getSex=function(){
return this.sex;
}
//*************************************
//公有静态方法调用
Person.show();
//公有方法调用
var p=new Person();
p.getSex();
//私有方法调用
p.checkSex("女");
</script>
</body>
</html>
JS(面向对象--模拟封装、继承)
<!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=gb2312" />
<title>博客日志</title>
</head>
<body>
<script>
function Person(){
this.name="lisi";
}
Person.prototype.setName=function(name){
this.name=name;
}
Person.prototype.getName=function(){
return this.name;
}
//模拟继承
function Student(){
}
Student.prototype=new Person();
var stu=new Student();
stu.setName("wangwu");
alert(stu.getName());
</script>
</body>
</html>
JS(面向对象--重载、重写)
<!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=gb2312" />
<title>博客日志</title>
</head>
<body>
<script>
function Person(){
//私有属性
this.name = "刘德华";
}
function Person(name){
this.name = name;
}
//共有方法
Person.prototype.getName = function(){
return this.name;
}
Person.prototype.setName = function(name){
this.name = name;
}
//------------------------------------------
//创建Student类
function Student(){
}
//---------------------------------------------
//让Student类继承Person类
var p = new Person();
Student.prototype = p;
//-------------------------------
//重写getName()、setName()方法
Student.prototype.getName = function(){
return "person";
}
Student.prototype.setName = function(name){
alert("haha");
}
//-----------------------------
var stu = new Student();
stu.setName("章子怡");
//alert (stu.getName() );
</script>
</body>
</html>
JS(特有语句)
<!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=gb2312" />
<title>博客日志</title>
</head>
<body>
<script>
var d=new Date();
with(d){
alert(getFullYear()+"年"+(getMonth()+1)+"月"+getDate()+"日");
}
function Person(){
this.name="lisi";
this.age=20;
}
var p=new Person();
for(x in p){
alert(p[x]);
}
</script>
</body>
</html>