如何修改本机Javascript对象的(原型?)方法?

时间:2010-11-09 06:38:20

标签: javascript

有没有它们的例子?

2 个答案:

答案 0 :(得分:2)

以下脚本在javascript中的字符串原型中添加了contains的方法,同样您可以根据需要添加更多方法。

<强>用法

var myName = "ZainShaikh";
if (myName.contains("zain", true)) { // this condition will be true, because of ignorecase parameter
     alert('yuppie, this is my name.');
 }
 else {
     alert('awww, this is not my name.');
}

原型方法

String.prototype.contains = function(value, ignorecase) {
    if (ignorecase) {
        return (this.toLowerCase().indexOf(value.toString().toLowerCase()) != -1);
    }
    else {
        return this.indexOf(value) != -1;
    }
};

答案 1 :(得分:-2)