JS面向对象编程实现

 

Function 在 中是一个很特殊的对象,其特殊性体现在它的多重身份。

Function 可以声明普通的函数,这一点和其他语言中的函数概念是相同的。除此以外,Function还可以用作类型的声明和实现、对象的构造函数,以及类引用。

Apply和Call方法可以将函数绑定到其它对象上执行。

使用for(…in…)语句可以遍历对象的所有属性和方法。如下面的例子就遍历了test1对象的属性和方法,如果是属性刚输出属性,如果是方法刚执行方法。

function test1()

{

       this.p1="p1";

       this.p2="p2";

       this.f1=function()

       {

              alert("f1");

       }

       this.f2=function()

       {

              alert("f2");

       }

}

var obj1=new test1();

//遍历t的所有属性

for(p in t)

{

       //如果是函数类型刚执行该函数

       if(typeof(t[p])=="function")

       t[p]();

       //输出其它类型的属性

       else alert(t[p]);

}

1.       类的声明

(1)、使用this关键字

function test1()

{

       this.p1="p1";

       this.p2="p2";

       this.f1=function()

       {

              alert("f1");

       }

       this.f2=function()

       {

              alert("f2");

       }

}

在中,成员变量没有私有与公有机制,但可以通过在类内部使用var来声明局部变量。其作用域是类定义的内部。

 

(2)、使用ptototype方式的类声明

如果要声明一个类的实例属性或方法,可以使用中的对象的prototype属性。例如:

Test1.prototype.prop2=”prop2”;

Test1.prototype.method2=function(){

    Alert(this.prop2);

}

使用prototype属性声明类,如下

function test()

{           

}

test.prototype={

       p1:"p1";

       p2:"p2";

       f1 : funciton ()

       {

              alert("f2");

       }

}

2.       继承

本身并没有提供继承的语法支持,但是在仍然可以采用复制属性和对象的方法实现继承。

function test2(){}

for(p in test.prototype)

{

test2.prototype[p]=test.prototype[p];

}

test2.prototype.newMethod=function()

{

alert("newMethod");

}

另外可以参考Prototype框架实现继承的方法:

object.extend=function (destination,source)

{

for(property in source)

{

        destination[property]=source[property];

}

return destiantion;

}

function test2() {}

test2.prototype=object.extend ({newMethod :function ()

{alert (“newMethod”);}

},

Test.prototype

);

3.       多态

多态的实现可以采用和继承类似的方法。首定义一个抽象类,其中可以,调 用一些虚方法,虚方法在抽象类中没有定义,而是通过其具体实现类来实现的。例如:

object.extend=function (destination,source)

{

for(property in source)

{

        destination[property]=source[property];

}

return destiantion;

}

//定义一个抽象基类

function base() {}

base.prototype={   

initialize:function(){

        this.oninit();//调用了一个虚方法

}

}

 

function test()

{

//构造函数

}

 

test.prototype=object.extend(

                                            {

                                                   prop:"prop",

                                                   oninit :function()

                                                   {

                                                          alert(this.prop)

                                                   }

                                            },base.prototype

                                            );

 

function test2()

{

}

 

test2.prototype=object.extend(

                                             {

                                                    prop2: "prop2";

                                                    oninit:function()

                                                    {

                                                           alert(this.prop2);

                                                    }

                                             },base.prototype

                                             }

posted @ 2009-08-04 14:47  ting_gt  阅读(269)  评论(0编辑  收藏  举报