Shuffy

不断的学习,不断的思考,才能不断的进步.Let's do better together!
posts - 102, comments - 43, trackbacks - 0, articles - 19

JS定义(构造)类的方法

Posted on 2012-04-27 20:58 Shuffy 阅读(735) 评论(0)  编辑 收藏 引用 所属分类: Web开发技术
                //1、构造函数+原型方法
                //构造函数
                //等价于Person=function(){}
                function Person(name,age){
                    
this.name=name;
                    
this.age=age;
                }

                
//原型构造(大括号的形式)
                Person.prototype={
                    gender:
"",
                    toString:
function(){
                        alert(
"name:"+this.name+",age:"+this.age);
                    }

                }
;
                
//原型构造
                Person.prototype.height=1.5;
                Person.prototype.toString
=function(){
                    alert(
"name:"+this.name+",age:"+this.age+",gender:"+this.gender+",height:"+this.height);
                }
;
                
var person=new Person("sandy",25);
                person.gender
="";
                person.height
=1.7;
                alert(person.name);
                alert(person.age);
                alert(person.gender);
                alert(person.height);
                person.toString();
                
                
//2、原型构造
                User=function(){};
                User.prototype
={
                    name:
"",
                    password:
"",
                    toString:
function(){
                        alert(
this.name+","+this.password);
                    }

                }
;
                
var user = new User();
                user.name
="admin";
                user.password
="admin";
                user.toString();
                
                
//通过原型和this关键字构造的属性和方法是公有的。
                //原型构造的缺点是当属性是对象类型时,该属性为引用类型,多个实例共享这一个属性,
                //其中一个实例改变了该属性之后,所有实例的该属性都被改变了。
                
                
//3、构造函数方法,可以通过下面类似OOP方式定义类
                //等价于function Car(sign,number){}
                Car=function(sign,number){
                    
//函数里的局部变量,用OOP的观点看,是私有属性。
                    var _sign=sign;
                    
var _number=number;
                    
                    
this.getSign=function(){
                        
return _sign;
                    }
;
                    
this.setSign=function(sign){
                        _sign
=sign;
                    }
;
                    
this.getNumber=function(){
                        
return _number;
                    }
;
                    
this.setNumber=function(number){
                        _number
=number;
                    }
;
                }
;
                
var car1 = new Car("宝马","1111111");
                
var car2 = new Car("欧迪","2333333");
                alert(car1.getSign()
+":"+car1.getNumber());
                alert(car2.getSign()
+":"+car2.getNumber());
                car1.setNumber(
"23223333");
                car2.setNumber(
"444444444");
                alert(car1.getSign()
+":"+car1.getNumber());
                alert(car2.getSign()
+":"+car2.getNumber());
                
                
//4、工厂方式
                //工厂方式1
                //function Song(){}
                Song=function(){
                    
//构造一个对象并返回(直接量方式构造一个对象)
                    var obj=new Object();
                    obj.author
="";
                    obj.name
="";
                    obj.play
=function(){
                        alert(
"author:"+obj.author+",name:"+obj.name);
                    }
;
                    
return obj;
                }
;
                
var song=Song();
                song.author
="Jay";
                song.name
="夜曲";
                song.play();
                
//工厂方式2
                function Music(){
                    
//新建一个匿名类的对象并返回
                    //var obj = new (function(){
                    //    this.type="";
                    //    this.year="";
                    //})();
                    //return obj;
                    return new (function(){
                        
this.type="";
                        
this.year="";
                    }
)();
                }

                
var m = Music();
                m.type
="爵士";
                m.year
="80年代";
                alert(m.type);
                alert(m.year);
                
//5、定义静态方法和属性,只能通过类名访问,类实例是访问不到的。
                Pen=function(){};
                Pen.DEFAULTCOLOR
="BLACK";
                
var pen = new Pen();
                
//alert(pen.DEFAULTCOLOR);
                alert(Pen.DEFAULTCOLOR);

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理