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 来声明它是局部的概念 )








posted on 2017-03-22 14:41  signheart  阅读(29309)  评论(0编辑  收藏  举报

导航