日期:2014-05-16  浏览次数:20313 次

javascript prototype(原型)
如果有什么不对的地方请指出
原型分为两种,一种是js 里的原型,还有就是原型框架。这里讲的是js里的原型。

//演示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