js 靜態方法 靜態變量 實例方法 實例變量


1、靜態方法的定義

 

Js代碼   收藏代碼
  1. var BaseClass = function() {}; // var BaseClass=new Function();  
  2. BaseClass.f1 = function(){//定義靜態方法  
  3.      alert(' This is a static method ');  
  4. }  
  5. BaseClass.f1();//This is a static method  
  6. var instance1 = new BaseClass();  
  7. instance1.f1();//instance1.f1 is not a function  

    由以上代碼分析可知,靜態方法不能被實例對象調用,再看以下代碼

 

Js代碼   收藏代碼
  1. var BaseClass = new Function;  
  2. var Class2 = BaseClass;  
  3. BaseClass.f1 = function(){  
  4. alert("BaseClass ' s static method");  
  5. }  
  6. Class2.f2 = function(){  
  7. alert("Class2 ' s static method");  
  8. }  
  9. BaseClass.f1();//BaseClass ' s static method  
  10. BaseClass.f2();//Class2 ' s static method  
  11. Class2.f1();//BaseClass ' s static method  
  12. Class2.f2();//Class2 ' s static method  

    從運行結果來看,BaseClass和Class都有f1和f2靜態方法,實際上這兩個函數是一樣的,可以執行以下代碼來驗證

 

Js代碼   收藏代碼
  1. alert(BaseClass == Class2);//true  

 

    如果刪除其中一個函數中的靜態方法,則對應的另一個函數的靜態方法也被刪除,比如執行

Js代碼   收藏代碼
  1. delete Class2.f2;  

    同時也會刪除BaseClass中的f2

 

2、實例方法的定義
    這里是利用JavaScript對象原型引用prototype來實現的,看以下代碼

 

Js代碼   收藏代碼
  1. var BaseClass = function() {};  
  2. BaseClass.prototype.method1 = function(){  
  3.       alert(' This is a instance method ');  
  4. }  
  5. var instance1 = new BaseClass();  
  6. instance1.method1();//This is a instance method  

    method1即為通過prototype原型引用定義的實例方法,這里也可以在實例上直接定義方法(變量),看以下代碼

 

Js代碼   收藏代碼
  1. var BaseClass = function() {};  
  2. var instance1 = new BaseClass();  
  3. instance1.method1 = function(){  
  4.     alert(' This is a instance method too ');  
  5. }   
  6. instance1.method1();//This is a instance method too  

   下面介紹通過this指針來定義實例方法(變量),看以下代碼

Js代碼   收藏代碼
  1. var BaseClass = function() {  
  2.  this.method1 = function(){  
  3.    alert(' Defined by the "this" instance method');  
  4.  }  
  5. };  
  6. var instance1 = new BaseClass();  
  7. instance1.method1();//Defined by the "this" instance method  

    那么同時在實例上、原型引用上和“this”上定義了相同名字的實例方法后,實例會優先調用那一個呢?請看以下代碼

Js代碼   收藏代碼
  1. var BaseClass = function() {  
  2. this.method1 = function(){  
  3.        alert(' Defined by the "this" in the instance method');  
  4.  }  
  5. };  
  6. var instance1 = new BaseClass();  
  7. instance1.method1 = function(){  
  8.     alert(' Defined directly in the instance method');  
  9. }  
  10. BaseClass.prototype.method1 = function(){  
  11.     alert(' Defined by the prototype instance method ');  
  12. }  
  13. instance1.method1();//Defined directly in the instance method  

    通過運行結果跟蹤測試可以看出直接定義在實例上的變量的優先級要高於定義在“this”上的,而定義在“this”上的又高於 prototype定義的變量。即直接定義在實例上的變量會覆蓋定義在“this”上和prototype定義的變量,定義在“this”上的會覆蓋prototype定義的變量。


3、內部方法
   先看以下定義


   
   
   
           
  1. var BaseClass = function() {
  2. var method1 = function() {
  3. alert("Internal method");
  4. };
  5. var method2 = function() {
  6. alert("call Internal method");
  7. method1();
  8. };
  9. this.method3 = function(){
  10. method2();
  11. }
  12. };
  13. var instance1 = new BaseClass();
  14. instance1.method1();// 會報錯,因為method1是BaseClass中定義的內部變量,作用域只有在內部可見(閉包)
  15. instance1.method3();//會先后調用method2和method1


另一篇記載


    
    
    
            
  1. /****************************************
  2. * 方法一
  3. * 類、方法、屬性都為靜態類型
  4. * 不能創建實例
  5. *****************************************/
  6. var Time = {
  7. today: 2009-3-8′,
  8. weather: rain’,
  9. show: function() {
  10. alert(‘Today is + this.today);
  11. }
  12. };
  13. //靜態對象可直接使用,無需創建實例
  14. alert(‘It is + Time.weather + today.’);
  15. Time.show();
  16. //下面的代碼會出錯,因為靜態類不能創建實例
  17. //var t = new Time();
  18. //t.show();
  19. /****************************************
  20. * 方法二
  21. * 普通對象,同時擁有靜態和非靜態屬性、方法
  22. * 可以用實例化
  23. * 注意:
  24. * 1.靜態方法/屬性使用類名訪問
  25. * 2.非靜態方法/屬性使用實例名訪問
  26. *****************************************/
  27. function Person(name) {
  28. //非靜態屬性
  29. this.name = name;
  30. //非靜態方法
  31. this.show = function() {
  32. alert(‘My name is + this.name + ‘.’);
  33. }
  34. }
  35. //添加靜態屬性,人都是一張嘴
  36. Person.mouth = 1;
  37. //添加靜態方法,哇哇大哭
  38. Person.cry = function() {
  39. alert(‘Wa wa wa …’);
  40. };
  41. //使用prototype關鍵字添加非靜態屬性,每個人的牙可能不一樣多
  42. Person.prototype.teeth = 32;
  43. //非靜態方法必須通過類的實例來訪問
  44. var me = new Person(‘Zhangsan’);
  45. //使用非靜態方法、屬性
  46. me.show();
  47. alert(‘I have + me.teeth + teeth.’);
  48. //使用靜態方法、屬性
  49. Person.cry();
  50. alert(‘I have + Person.mouth + mouth.’);

總結  
1.聲明 靜態變量,對象類.變量名, 對象類. 靜態方法,不能使用實例調用(
    1.也可以不用對象類.來聲明,但是一定不要使用var 來聲明,在類 用 var聲明的方法或者變量都是局部的,不用的話全局的也就是這個類的靜態變量
)


2.聲明非靜態的變量或方法  ,( 
1.可以用this.聲明   , 
2. 也可以使用對象類. prototype 來聲明,
3.也可以不用this. 來聲明變量或者方法,但是聲明變量時要加入var 來聲明它是局部的概念 )









免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM