修改对象实例代码


//create new method
Number.prototype.toHexString=function(){
return this.toString(16);
};
var iNum=15;
alert(iNum.toHexString());//f

Array.prototype.equeue=function(vItem){
return this.push(vItem);
};
var aColor=["red","green"];
aColor.equeue("blue");
alert(aColor);//red,green,blue

Array.prototype.dequeue=function(){
return this.shift();
};
aColor.dequeue();
alert(aColor);//green,blue
Array.prototype.indexof=function(vItem){
for(i=0;i<this.length;i++){
if(vItem==this[i]){
return i;
}
}
return -1;
}
var oColor=["red","green","blue"];
alert(oColor.indexof("blue"));//2
Object.prototype.showValue=function(){
alert(this.valueOf());
};
var sString="green";
var iNum=23;
sString.showValue();//green
iNum.showValue();//23

//函数名只是指向函数的指针,因此可以修改该指针指向的函数。重定义已有的方法。
//re definite method
Function.prototype.toString=function(){
alert("function code hidden!");
};
function sayHi(){
alert("Hi");
}
sayHi.toString();//function code hidden!

Function.prototype.originaltoString=Function.prototype.toString;
Function.prototype.toString=function(){
if(this.originaltoString().length>=100){
alert("Function too long to display");
}
alert(this.originaltoString());
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值