1、靜態方法的定義
- var BaseClass = function() {}; // var BaseClass=new Function();
- BaseClass.f1 = function(){//定義靜態方法
- alert(' This is a static method ');
- }
- BaseClass.f1();//This is a static method
- var instance1 = new BaseClass();
- instance1.f1();//instance1.f1 is not a function
由以上代碼分析可知,靜態方法不能被實例對象調用,再看以下代碼
- var BaseClass = new Function;
- var Class2 = BaseClass;
- BaseClass.f1 = function(){
- alert("BaseClass ' s static method");
- }
- Class2.f2 = function(){
- alert("Class2 ' s static method");
- }
- BaseClass.f1();//BaseClass ' s static method
- BaseClass.f2();//Class2 ' s static method
- Class2.f1();//BaseClass ' s static method
- Class2.f2();//Class2 ' s static method
從運行結果來看,BaseClass和Class都有f1和f2靜態方法,實際上這兩個函數是一樣的,可以執行以下代碼來驗證
- alert(BaseClass == Class2);//true
如果刪除其中一個函數中的靜態方法,則對應的另一個函數的靜態方法也被刪除,比如執行
- delete Class2.f2;
同時也會刪除BaseClass中的f2
2、實例方法的定義
這里是利用JavaScript對象原型引用prototype來實現的,看以下代碼
- var BaseClass = function() {};
- BaseClass.prototype.method1 = function(){
- alert(' This is a instance method ');
- }
- var instance1 = new BaseClass();
- instance1.method1();//This is a instance method
method1即為通過prototype原型引用定義的實例方法,這里也可以在實例上直接定義方法(變量),看以下代碼
- var BaseClass = function() {};
- var instance1 = new BaseClass();
- instance1.method1 = function(){
- alert(' This is a instance method too ');
- }
- instance1.method1();//This is a instance method too
下面介紹通過this指針來定義實例方法(變量),看以下代碼
- var BaseClass = function() {
- this.method1 = function(){
- alert(' Defined by the "this" instance method');
- }
- };
- var instance1 = new BaseClass();
- instance1.method1();//Defined by the "this" instance method
那么同時在實例上、原型引用上和“this”上定義了相同名字的實例方法后,實例會優先調用那一個呢?請看以下代碼
- var BaseClass = function() {
- this.method1 = function(){
- alert(' Defined by the "this" in the instance method');
- }
- };
- var instance1 = new BaseClass();
- instance1.method1 = function(){
- alert(' Defined directly in the instance method');
- }
- BaseClass.prototype.method1 = function(){
- alert(' Defined by the prototype instance method ');
- }
- instance1.method1();//Defined directly in the instance method
通過運行結果跟蹤測試可以看出直接定義在實例上的變量的優先級要高於定義在“this”上的,而定義在“this”上的又高於 prototype定義的變量。即直接定義在實例上的變量會覆蓋定義在“this”上和prototype定義的變量,定義在“this”上的會覆蓋prototype定義的變量。
3、內部方法
先看以下定義
var BaseClass = function() {
var method1 = function() {
alert("Internal method");
};
var method2 = function() {
alert("call Internal method");
method1();
};
this.method3 = function(){
method2();
}
};
var instance1 = new BaseClass();
instance1.method1();// 會報錯,因為method1是BaseClass中定義的內部變量,作用域只有在內部可見(閉包)
instance1.method3();//會先后調用method2和method1
另一篇記載
/****************************************
* 方法一
* 類、方法、屬性都為靜態類型
* 不能創建實例
*****************************************/
var Time = {
today: ‘2009-3-8′,
weather: ‘rain’,
show: function() {
alert(‘Today is ‘ + this.today);
}
};
//靜態對象可直接使用,無需創建實例
alert(‘It is ‘ + Time.weather + ‘ today.’);
Time.show();
//下面的代碼會出錯,因為靜態類不能創建實例
//var t = new Time();
//t.show();
/****************************************
* 方法二
* 普通對象,同時擁有靜態和非靜態屬性、方法
* 可以用實例化
* 注意:
* 1.靜態方法/屬性使用類名訪問
* 2.非靜態方法/屬性使用實例名訪問
*****************************************/
function Person(name) {
//非靜態屬性
this.name = name;
//非靜態方法
this.show = function() {
alert(‘My name is ‘ + this.name + ‘.’);
}
}
//添加靜態屬性,人都是一張嘴
Person.mouth = 1;
//添加靜態方法,哇哇大哭
Person.cry = function() {
alert(‘Wa wa wa …’);
};
//使用prototype關鍵字添加非靜態屬性,每個人的牙可能不一樣多
Person.prototype.teeth = 32;
//非靜態方法必須通過類的實例來訪問
var me = new Person(‘Zhangsan’);
//使用非靜態方法、屬性
me.show();
alert(‘I have ‘ + me.teeth + ‘ teeth.’);
//使用靜態方法、屬性
Person.cry();
alert(‘I have ‘ + Person.mouth + ‘ mouth.’);
總結
1.聲明 靜態變量,對象類.變量名,
對象類.
靜態方法,不能使用實例調用(
1.也可以不用對象類.來聲明,但是一定不要使用var 來聲明,在類 用 var聲明的方法或者變量都是局部的,不用的話全局的也就是這個類的靜態變量
)
2.聲明非靜態的變量或方法 ,(
1.可以用this.聲明 ,
2. 也可以使用對象類.
prototype 來聲明,
3.也可以不用this. 來聲明變量或者方法,但是聲明變量時要加入var 來聲明它是局部的概念 )

