JavaScriptオブジェクト向け(3)--prototypeと継承

10444 ワード

1、prototypeによる継承
 1 function BaseClass()

 2 {

 3     var privateName = "private";

 4     

 5     this.pubicName1 = "public1";

 6     

 7     var privateMethod = function()

 8     {

 9         alert("privateMethod");

10     }

11     

12     this.publicMethod1 = function()

13     {

14         alert("publicMethod1");

15     }

16     

17     this.alertPrivateName = function()

18     {

19         alert(privateName);

20         privateMethod();

21     }

22     

23 }

24 

25 BaseClass.prototype.publicName2 = "public2";

26 BaseClass.prototype.publicMethod2 = function()

27 {

28     alert("publicMethod2");

29 }

30 

31 BaseClass.staticName = "static";

32 BaseClass.staticMethod = function()

33 {

34     alert("staticMethod");

35 }

36 

37 MyClass = function()

38 {

39     this.getName = function()

40     {

41         //              

42         alert(privateName);

43         privateMethod();

44     }

45 }

46 

47 MyClass.prototype = new BaseClass();

48 object = new MyClass();

49 

50 //          

51 alert(object.pubicName1);

52 alert(object.publicName2);

53 //          

54 alert(object.constructor.staticName);

55 //          

56 object.publicMethod1();

57 object.publicMethod2();

58 //          

59 object.constructor.staticMethod();

60 //                      

61 object.alertPrivateName();

 
MyClass.prototype = new BaseClass()   MyClass BaseClass   。MyClass           、    、    、    ,                          。          !

 
2、私を使って継承を実現する
function BaseClass()

{

    var privateName = "private";

    

    this.pubicName1 = "public1";

    

    var privateMethod = function()

    {

        alert("privateMethod");

    }

    

    this.publicMethod1 = function()

    {

        alert("publicMethod1");

    }

    

    this.alertPrivateName = function()

    {

        alert(privateName);

        privateMethod();

    }

    

}



BaseClass.prototype.publicName2 = "public2";

BaseClass.prototype.publicMethod2 = function()

{

    alert("publicMethod2");

}



BaseClass.staticName = "static";

BaseClass.staticMethod = function()

{

    alert("staticMethod");

}





MyClass = function()

{

    var me = new BaseClass();

    

    me.getName = function()

    {

        //              

        alert(privateName);

        privateMethod();

    }

    

    //          

    me.otherMethod = function()

    {

        alert("otherMethod");

    }

    

    return me;

}


//Prototype定義の方法でMyClassにアクセスできません.prototype.otherMethod2 = function()  {    alert("otherMethod2");  };
object = new MyClass();



//          

alert(object.pubicName1);

alert(object.publicName2);

//          

alert(object.constructor.staticName);

//          

object.publicMethod1();

object.publicMethod2();

//          

object.constructor.staticMethod();

//                      

object.alertPrivateName();

 
この継承方式の効果は基本的に1つ目と同じですが、この継承方式は関数体の内部でmeに属性と方法を追加するだけで、prototypeを使用することはできません.new MyClass()の時に戻ってくるのは実は属性と方法を追加したmeなので、MyClassはただの殻です.