js中的prototype与constructor

来源:互联网 发布:java 线程同步卖票 编辑:程序博客网 时间:2024/05/16 05:38

 

  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定义了变量后,用于附加到类上

原创粉丝点击