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";
}

 

posted @ 2013-08-20 11:18  bupt_ding  阅读(511)  评论(0)    收藏  举报