IE9中的this = DispHTMLWindow2在原型's函数中

IE9 this = DispHTMLWindow2 inside a prototype's function

本文关键字:函数 原型 this 中的 DispHTMLWindow2 IE9      更新时间:2023-09-26

我在js中创建了一个名为startsWith的扩展函数

它是这样的:

String.prototype.startsWith = function (str) {
 if (this.indexOf(str) == 0) {
     return true;
 }
 return false;
}

当在IE9中运行时,我得到一个错误说:"对象不支持属性或方法'indexOf'"。

当在调试器中查看时,它似乎是DispHTMLWindow2。

帮忙吗?

谢谢。

您可以尝试以下从本网站获取的内容

String.prototype.startsWith = function(str) 
{return (this.match("^"+str)==str)}
String.prototype.endsWith = function(str) 
{return (this.match(str+"$")==str)}
String.prototype.trim = function(){return 
(this.replace(/^['s'xA0]+/, "").replace(/['s'xA0]+$/, ""))}