web前端之面向对象

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>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值