js中的prototype与constructor

 

  1. <script>
  2. function Rectangle(w,h)
  3. {
  4.     this.width=w;
  5.     this.height=h;
  6.     this.area=function(){return this.width*this.height+1;}
  7. }
  8. Rectangle.prototype.area=function(){return this.width*this.height+2;}
  9. var test=new Rectangle(5,10);
  10. test.area=function(){return this.width*this.height;}
  11. alert(test.area())
  12. alert(test.constructor.constructor)
  13. alert(test.constructor)
  14. alert(test.constructor.constructor)
  15. alert(test.constructor.prototype)
  16. alert(Rectangle.constructor.prototype)
  17. </script>

在js中的类,只写构造function
对象test.area()首先中调用自已对象的area()方法,

然后在自已的类中找

如果没有则在类的static属性prototype中找

 

2.第个类都可以当作方法直接调用

function Rectangle(w,h)
{
 this.width=w;
    this.height=h;
    this.area=function(){return this.width*this.height+1;};
 alert(111)
}

Rectangle(5,10);

 

3.
this.area是一般预定义是用
prototype定义了变量后,用于附加到类上

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值