Javascript方法小札记

1静态方法

 

存储在运行内存上的静态区域,该类方法由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 

2对象方法

对象方法即创建对象时,以属性的方式创建的方法,同时加入了该对象的原型链中。

3原型方法

一般用于既存的对象,扩展该对象或者扩展继承该对象的对象。将方法动态的添加到对象的原型链中。

  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定义的变量。

根据运行测试,可以判断,对象方法创建时,是将方法加入原型链堆栈的最上层,而实例对象中,原型的调用又相对于自身对象方法处于底层。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值