Fork me on GitHub

JavaScript 中的类方法,对象方法,Prototype方法

 1 <script type="text/javascript">
 2  function baseClass()
 3 {
 4     this.showMsg = function()
 5     {
 6         alert("baseClass::showMsg");   
 7     }
 8    
 9     this.baseShowMsg = function()
10     {
11         alert("baseClass::baseShowMsg");
12     }
13 }
14 baseClass.showMsg = function()
15 {
16     alert("baseClass::showMsg static");
17 }
18 
19 function extendClass()
20 {
21     this.showMsg =function ()
22     {
23         alert("extendClass::showMsg");
24     }
25 }
26 extendClass.showMsg = function()
27 {
28     alert("extendClass::showMsg static")
29 }
30 
31 extendClass.prototype = new baseClass();
32 var instance = new extendClass();
33 
34 instance.showMsg(); //显示extendClass::showMsg
35 instance.baseShowMsg(); //显示baseClass::baseShowMsg
36 instance.showMsg(); //显示extendClass::showMsg
37 
38 baseClass.showMsg.call(instance);//显示baseClass::showMsg static
39 
40 var baseinstance = new baseClass();
41 baseinstance.showMsg.call(instance);//显示baseClass::showMsg
42 </script>

 

posted @ 2015-04-30 10:14  Terry√  阅读(308)  评论(0编辑  收藏  举报