1. 程式人生 > >JavaScript權威指南 第一章 程式碼總結

JavaScript權威指南 第一章 程式碼總結

var points = [{x:0,y:0},{x:1,y:1}]

points.dist=function(){var p1=this[0];var p2=this[1];var a=p2.x-p1.x;var b=p2.y-p1.y;return Math.sqrt(a*a+b*b);}

points.dist();

寫的時候沒有注意到這是為陣列建立的方法。


function Point(x,y){this.x=x;this.y=y;} //建立建構函式

var p=new Point(1,1);//例項化

Point.prototype.r=function(){return Math.sqrt(this.x*this.x+this.y*this.y);}

p.r();