web前端之面向对象
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Document</title>
<script type="text/javascript">
var obj={};
obj.name="FZW";
obj.code="18";
obj.showName=function(){
alert("我的名字叫:"+this.name);
};
obj.showCode=function(){
alert("code:"+this.code);
};
obj.showName();
obj.showCode();
</script>
</head>
<body>
</body>
</html>
一个简单的面向对象
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Document</title>
<script type="text/javascript">
var obj={};
obj.name="FZW";
obj.code="18";
obj.showName=function(){
alert("我的名字叫:"+this.name);
};
obj.showCode=function(){
alert("code:"+this.code);
};
obj.showName();
obj.showCode();
</script>
</head>
<body>
</body>
</html>
使用this来面向对象
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Document</title>
<script type="text/javascript">
function newObj(name,code){
// var obj={};
this.name=name;
this.code=code;
this.showName=function(){
alert("我的名字叫:"+this.name);
};
this.showCode=function(){
alert("code:"+this.code);
};
// return obj;
};
var obj=new newObj("lalala",18);
obj.showCode();
obj.showName();
</script>
</head>
<body>
</body>
</html>
css
class 一次给一组元素加样式 原型
行间样式 一次给一个元素加样式 给对象加东西
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Document</title>
<script type="text/javascript">
var arr1=[1,2,3,1,12,2];
var arr2=[12,23,11];
// arr1.sum=function(){
// var result=0;
// var that=this;
// that.forEach(function(value,index){
// result+=that[index];
// });
// return result;
// }
// alert(arr1.sum());相当于行间样式
// alert(arr2.sum());
Array.prototype.sum=function(){
var result=0;
var that=this;
that.forEach(function(value,index){
result+=value;
});
return result;
};
alert(arr1.sum());
alert(arr2.sum());
</script>
</head>
<body>
</body>
</html>
面向对象:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Document</title>
<script type="text/javascript">
function newObj(name,code){
// var obj={};
this.name=name;
this.code=code;
// 在构造方法加属性
};
newObj.prototype.showName=function(){//在原型里面加事件
alert("我的名字叫:"+this.name);
};
newObj.prototype.showCode=function(){
alert("code:"+this.code);
};
var obj=new newObj("lalala",18);
obj.showCode();
obj.showName();
var obj1=new newObj("123",123);
obj1.showCode();
obj2.showName();
</script>
</head>
<body>
</body>
</html>