javascript prototype(原型)

[color=red][b]如果有什么不对的地方请指出[/b][/color]
原型分为两种,一种是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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值