js Circle类
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>index</title> <meta name="author" content="Administrator" /> <script type="text/javascript" src="yuan.js"></script> <!-- Date: 2015-12-29 --> <script type="text/javascript"> var r = 5; var circle = new Circle(r); document.write("半径为"+r+"的周长:"+circle.perimeter()+",面积:"+ circle.area()); </script> </head> <body> </body> </html>
yuan.js文件代码:
/** * 创建一个对象圆 * 属性:半径 * 方法:面积和周长 * @author Administrator */ function Circle(r){ this.r = r; } Circle.prototype.area = function(){ return this.r * this.r; }; Circle.prototype.perimeter = function(){ return 2 * Math.PI * this.r; };