js prototype

最近一段时间,研究公司的平台,平台比较老了,还用的很老的js ,ajax也是用的原理级的东西,js底层的东西相对来说比较复杂,没有我们想的那个简单,当时看到了一个prototype这个属性,当时不理解,再后来的时间里有细细的研究了一遍,这个是理解代码与大家分享。

 

  /* 实例一
   function People()
  {
        this.Jump=function(){
            alert("I can jump");
    }
  }
 People.prototype.Run=function(){
        alert("I can run,too");
 }
 var p = new People();
 p.Jump();
 p.Run();
 */
 /* 实例2 理解类方法,对象方法,原型方法
 function People(name){
  this.name = name;
  this.indface= function(){
   alert("my name is "+this.name);
  }
 }
 
 People.Run = function(){
  alert("hello i am run !");
 }
 
 People.prototype.indfaceChinese=function(){
  alert("hello my name is "+this.name+" too.");
 }
 
 var p = new People("john");
 p.indface();
 People.Run();
 p.indfaceChinese();
 */
 /* 实例三 继承
 function baseclass(){
  this.say = function (){
   alert("baseclass:showmessage");
  }
 }
 
 function extendclass(){
 
 }
 
 extendclass.prototype = new baseclass();
 
 var instance = new extendclass();
 instance.say();
 */
 /** 实例四 继承的重写
 function baseclass(){
  this.say = function(){
   alert("baseclass : show message");
  }
 }
 
 function extendclass(){
  this.say = function(){
   alert("extendclass : show message");
  }
 }
 
 extendclass.prototype = new baseclass();
 
 var instance = new extendclass();
 instance.say();
 */
 /* 实例五 综合体现了 prototype的所有的属性
 function baseClass(){
  this.showMessage = function(){
   alert("show message!");
  }
  
  this.baseShowMessage = function(){
   alert("base show message!");
  }
 }
 
 baseClass.showMessage=function(){
  alert("cccccc static!");
 }
 
 function extendClass(){
  this.showMessage = function(){
   alert("extend show message");
  }
 }
 
 extendClass.showMessage= function(){
  alert("show extend show message static!");
 }
 
 extendClass.prototype = new baseClass();
 
 var instance = new extendClass();
 //重写了父类的方法
 instance.showMessage();
 //继承了父类的方法
 instance.baseShowMessage();
 //继承类调用类方法(可以理解成不能继承的静态的方法)
 extendClass.showMessage();
 //父类调用对象类方法(可以理解成不能继承的静态的方法)
 baseClass.showMessage(); 
 //通过父类调用call的方法执行对象方法
 baseClass.showMessage.call(instance);
 //通过创建父类的对象然后去调用对象方法的call的形式
 var baseInstance = new baseClass();
 
 baseInstance.showMessage.call(instance);
 */

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值