小秋的家

home

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  1 Posts :: 91 Stories :: 17 Comments :: 0 Trackbacks

        javascript面向对象编程实现:
            1.类的声明:
                function test1(){
                    this.p1 = "p1";
                    this.p2 = "p2";
                    this.f1 = function(){ alert("f1"); }
                    this.f2 = function(){ akert("f2"); }
                }
                上面的代码声明了两个公有的属性p1和p2,两个方法f1和f2.
                如何申明私有变量呢?
                function test1(){
                    var _test = "test";
                    this.p1 = "p1";
                    this.p2 = "p2";
                    this.f1 = function(){ alert("f1"); }
                    this.f2 = function(){ akert("f2"); }
                }
                上面的代码通过var关键字声明了一个局部变量_test,其作用域是test1类定义的内部,对外不公开.
                如何申明类的静态变量和静态方法呢?
                test1.staticProp = "static var";
                test1.staticMethod = function(){
                    alert("static method");
                }
                还可以通过javascript中对象的prototype属性来申明类的实例属性或方法:
                test1.prototype.prop2 = "prop2";
                test2.prototype.method2 = function(){
                    alert(" this.prop2");
                }
`               利用prototype属性,可以实现另一种类的声明方法:
                function test(){}
                test.prototype = {
                    p1 : "p1",
                    p2 : "p2",
                    f1 : function(){
                        alert("f1");
                    }
                  }
                
                如何实现类的继承呢?通过复制父类的所有属性和方法来实现子类的继承:
                用for(....in ....)方法来实现遍历父类所有的属性和方法
                这里看看我如何让新声明的test1类继承test类的:
                function test1(){}//定义新声明的test1类.
                var p;                
                for(p in test.prototype){    //遍历父类的所有属性和方法
                    test1.prototype[p] = test.prtotype[p];   //把父类的所有属性和方法全部复制到新声明的子类test1中
                }
                test1.prototype.newMethod = function(){//定义新声明的子类test1的新方法
                    alert("new method");
                }
                其实prototype框架已经帮我们实现了这种继承,我们来看看它是如何实现的:
                Object.extend = function(destination, source){
                    for(property in source){
                        destination[property] = source[property];
                    }
                    return destination;
                 }
                Prototype框架为Object对象定义了extend方法,该方法有两个参数destination和source,分别对应于子类和父类,所以我们前面所讲的test1继承test可以简化代码如下:
                 function test1(){
                    test1.prototype = Object.extend({
                        newMethod : function(){
                            alert("new method");
                        }
                    },
                    test.prototype
                );
            
                如果我们改变一下上述代码中extend方法中的两个参数的顺序:
               function test1(){
                    test1.prototype = Object.extend(
                        test.prototype,
                        {
                        newMethod : function(){
                            alert("new method");
                        }
                    },
                );  
                   可以发现,对于test1子类来说效果是相同的..
                    但是,我们进一步会发现,父类test确拥有了子类test1的新增方法,虽然这不是我们最初想要的继承效果,但是这种方法却使我们有了扩展对象属性或者方法的手段了,不是吗?

posted on 2008-08-27 14:53 棋剑小秋 阅读(151) 评论(0)  编辑  收藏 所属分类: AJAX

只有注册用户登录后才能发表评论。


网站导航: