Javascript 修改对象
1.重命名已有方法
Array.prototype.enqueue = function(vItem) { this.push(vItem); }; Array.prototype.dequeue = function() { return this.shift(); };
2.添加方法
Array.prototype.indexOf = function (vItem) { for (var i=0; i<this.length; i++) { if (vItem == this[i]) { return i; } } return -1; }
3.为本地对象添加新方法
如果想给每个本地对象添加新方法,必须在 Object 对象的 prototype 属性上定义它。
Object.prototype.showValue = function () { alert(this.valueOf()); }; var str = "hello"; var iNum = 25; str.showValue(); //输出 "hello" iNum.showValue(); //输出 "25"
4.重定义已有的方法
Function.prototype.toString = function() { return "Function code hidden"; }

浙公网安备 33010602011771号