OOP in javascript-模仿类

  1. javascript中模仿类
    function Circle () {
       
    }

      上面的代码就声明了一个类,准确说是一个对象构造器(constructor),这是一个空类,没有任何属性和方法,没有什么意义,我们可以添加需要的属性和方法。
  2. 属性和方法
    function  Circle (xPoint, yPoint, radius)  {
        
    this.x = xPoint;  // The x component of the center of the circle.
        this.y = yPoint;  // The y component of the center of the circle.
        this.r = radius;  // The radius of the circle.
          this.getArea()=function(){Math.PI*this.r*this.r;};  //return area of the circle.

    circle包含3个属性和一个方法,x,y表示坐标,r表示半径,getArea方法返回圆的面积。
  3. 实例化
    var  circle  =   new  Circle( 1 , 2 , 5 ); // instantiate a Circle object.
    var  circleArea  =  circle.getArea(); // get the area of the circle;
    通过关键字来实例化一个类,对象点操作符来访问对象方法和属性。

 

转载于:https://www.cnblogs.com/AganCN/archive/2008/01/12/1036182.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值