Moris' Note Book

本博客所有内容皆收集于网上,非本人原创,非心情日记,非研究心得,只是自己浏览的东西的收集
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Some hints for JavaScript object model

Posted on 2006-04-12 10:15  moris  阅读(105)  评论(0)    收藏  举报

function Shape(type) {

  this.type = type;

  this.area = function ( ) {

    return "I have no idea";

  }

}

Rectangle.prototype = new Shape("Rectangle"); Rectangle.prototype.constructor = Rectangle;

function Rectangle(w, h) {

  this.width = w;

  this.height = h;

  this.area = function ( ) {

     return this.width * this.height;

  }

}

Circle.prototype = new Shape("Circle");

Circle.prototype.constructor = Circle;

function Circle(radius) {

  // The constructor defines the class itself.

  // r is an instance property, defined and initialized in the constructor.

  this.r = radius;

  this.PI = 3.1415926;

  this.area = function ( ) {

    return this.PI * this.r * this.r;

  }

}

rect = new Rectangle(3,1);

circle = new Circle(3);

shape = new Shape("Shape");

aShape = {"rect":rect,"circle":circle,"shape":shape};

output(aShape);

function output(o) {

  for( s in o) {

    document.write("type="+o[s].type+"");

    document.write("area="+o[s].area()+"");

  }

}