日期:2014-05-16 浏览次数:20339 次
//演示prototype原型的用法,其实它等价于java中的类变量或者是静态变量 //在原型里仅定义一些方法(方法一般是不会变的),常量等 function Circle(x, y, z) { this.x = x; this.y = y; this.z = z; } new Circle(0, 0, 0); Circle.prototype.pi = 3.14159; function Circle_circumference(){ return 2 * this.pi * this.r; } Circle.prototype.circumference = Circle_circumference; Circle.prototype.area = function(){ return this.pi * this.r * this.r; } var c = new Circle(0.0, 0.0, 1.0); var a = c.area(); var p = c.circumference(); //ttt 演示用prototype给系统内置的String添加函数 String.prototype.endsWith = function(c) { return (c == this.charAt(this.length-1)) } var message = "hello world"; message.endsWith('h') // Returns false message.endsWith('d') // Returns true