修改对象实例代码

本文介绍了一些实用的JavaScript方法扩展,包括将数字转换为十六进制字符串、数组的enqueue和dequeue操作、查找数组元素索引及对象显示其值的方法。通过这些扩展,可以更方便地进行JavaScript开发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


//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());
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值