面向对象1

来源:互联网 发布:常见办公软件 编辑:程序博客网 时间:2024/06/08 13:57
<!DOCTYPE html><html><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><title>Examples</title><meta name="description" content=""><meta name="keywords" content=""><link href="" rel="stylesheet"><script>/*属性,变量,函数,方法*/var arr=[12,13,56,34];// alert(typeof arr);var a= 23;// arr.a=4;// alert(arr.a);function show(){alert(a);}arr.fn=function(){alert(a)}show();arr.fn();/*原始*/var obj=new Object();obj.name='csq';obj.sex='男';obj.showname=function(){alert('我的姓名是:'+obj.name);}obj.showsex=function(){alert('我的性别是:'+obj.sex);}obj.showname();obj.showsex();/*工厂模式*/function createperson(name,sex){//原料var obj=new Object();//加工obj.name=name;obj.sex=sex;obj.showname=function(){alert('我的名字是'+obj.name);}obj.showsex=function(){alert('我的性别是:'+obj.sex);}//出厂return obj;}var p1=createperson('csq','男');p1.showname();p1.showsex();var p2=createperson('clf','女');p2.showname();p2.showsex();alert(p1.showname==p2.showname);/*原型prototype*/function creatperson(name,sex){     this.name=name;     this.sex= sex;}//构造函数=类,creatperson.prototype.showname=function(){alert('我的名字是:'+this.name);}creatperson.prototype.showsex=function(){alert('我的性别是:'+this.sex);}var p =new creatperson('csq','男');p.showname();p.showsex();//构造函数写属性(变),原型写方法(不变)</script></head><body>    </body></html>

0 0