1 /* 2 *对象方法 3 *类方法 4 * 原型方法 5 */ 6 function People(name) { 7 this.name = name; 8 this.say = function () { //对象方法 9 alert("my name is "+this.name); 10 } 11 } 12 13 People.run = function () { //类方法(静态方法,只能由类名调用) 14 alert("i can run"); 15 } 16 17 People.prototype.sayChinese = function () {//原型方法 18 alert("我的名字是:"+this.name); 19 } 20 21 var p1 = new People("menglinghua"); 22 p1.say(); 23 People.run(); 24 p1.sayChinese(); 25 26 p1.prototype //无法获取???????? 27 p1.run(); //无法调用??? 28 p1.sayChinese(); //可以调用 29 30 People.say();//无法调用???????? 31 People.sayChinese();//无法调用???????? 32 33 People.prototype.say();//无法调用???????? 34 People.prototype.run();//无法调用???????? 35 36 37 /* 38 *eg1 39 */ 40 function baseClass() 41 { 42 this.showMessage = function () { 43 alert("baseClass::showMessage()"); 44 } 45 } 46 47 function extendClass() 48 { 49 50 } 51 52 extendClass.prototype = new baseClass(); 53 var extendClassObj = new extendClass(); 54 extendClassObj.showMessage(); //输出"baseClass::showMessage()" 55 56 57 /* 58 *eg2 两个类含有同名方法 59 */ 60 function baseClass() { 61 this.showMessage = function () { 62 alert("baseClass::showMessage( )"); 63 } 64 } 65 66 function extendClass() { 67 this.showMessage = function () { 68 alert("extendClass::showMessage( )"); 69 } 70 } 71 72 extendClass.prototype = new baseClass(); 73 var extendClassObj = new extendClass(); 74 extendClassObj.showMessage(); //输出"extendClass::showMessage( )" 75 76 /* 77 *eg3 让extendClass的实例调用baseClass的同名方法 78 */ 79 function baseClass() { 80 this.showMessage = function () { 81 alert("baseClass::showMessage( )"); 82 } 83 } 84 85 function extendClass() { 86 this.showMessage = function () { 87 alert("extendClass::showMessage( )"); 88 } 89 } 90 91 extendClass.prototype = new baseClass(); 92 var extendClassObj = new extendClass(); 93 var baseClassObj = new baseClass(); 94 baseClassObj.showMessage.call(extendClassObj);//让【extendClassObj】调用【baseClassObj】的【showMessage】方法 95 //输出:"baseClass::showMessage( )" 96 97 /* 98 *eg4 99 */ 100 function baseClass() 101 { 102 this.showMessage = function () { 103 alert("baseClass::showMessage( )"); 104 } 105 this.baseShowMessage = function () { 106 alert("baseClass::baseShowMessage"); 107 } 108 } 109 baseClass.showMessage = function () { 110 alert("baseClass::showMessge static"); 111 } 112 113 function extendClass() 114 { 115 this.showMessage = function () { 116 alert("extendClass::showMessage( )"); 117 } 118 } 119 120 extendClass.prototype = new baseClass(); 121 var extendClassObj = new extendClass(); 122 123 extendClassObj.showMessage();//输出:extendClass::showMessage( ) 124 extendClassObj.baseShowMessage();//输出:baseClass::baseShowMessage 125 126 baseClass.showMessage.call(extendClassObj);//输出:baseClass::showMessge static 127 128 var baseClassObj = new baseClass(); 129 baseClassObj.showMessage.call(extendClassObj);//输出:baseClass::showMessage( )
http://www.cnblogs.com/yjf512/archive/2011/06/03/2071914.html