使用正则表达式时出现 Javascript 错误 - 对象不支持此属性或方法
Javascript Error when using regexp - Object doesn't support this property or method
所以我正在阅读这本书并逐字复制代码以动手使用它,我得到"对象不支持此属性或方法"。
var text = '<html><body bgcolor=blue><p>' + '<This is <b>BOLD<'/b>!<'/p><'/body><'/html>';
var tags = /[^<>]+|<('/?)([A-Za-z]+)([^<>]*)>/g;
var a,i;
String.method('entityify', function () {
var character = {
'<': '<',
'>': '>',
'&': '&',
'"': '"'
};
return function() {
return this.replace( /[<>&"]/g , function(c) {
return character[c];
});
};
}());
while((a = tags.exec(text))) {
for (i = 0; i < a.length; i += 1) {
document.writeln(('// [' + i + '] ' + a[i]).entityify());
}
document.writeln();
}
//Output [0] <html>
//Output [1]
//Output [2] html
//Output [3]
//and so on through the loop.
我似乎无法让他们的例子起作用。
**编辑 - 我找到并添加了该功能,但仍然无法正常工作。
问题是没有String.method(...)
函数。 如果您尝试向字符串类型添加新函数,请尝试以下操作:
String.prototype.entityify = (function () {
var character = {
'<':'<', '>':'>', '&':'&', '"':'"'
};
return function() {
return this.replace( /[<>&"]/g , function(c) {
return character[c];
});
};
})();
'<foo & bar>'.entityify(); // => "<foo & bar>"
虽然,如果您打算将此部分作为库的一部分,则不应直接分配给String.prototype
,而应使用此处所示的Object.defineProperty(...)
。
相关文章:
- 对象不支持属性或方法“自动完成”
- 对象没有'不支持set_viewXML
- 对象不支持对象IE 8
- 如何从 MVC4 链接调用 Javascript 方法(错误 JavaScript 运行时错误:对象不支持此操作)
- JSHint 错误“对象不支持此属性或方法”用于 javascript 闭包
- 对象不支持属性或方法“确认”
- 对象不支持属性或方法“jscomp_symbol_iterator0”
- 对象不支持 igGrid 中的属性或方法“_super”
- 下拉 Javascript 错误:对象不支持属性或方法“匹配”
- 运行时错误:对象不支持属性或方法
- Javascript 错误:对象在 IE 浏览器中不支持此属性或方法
- 对象不支持在调用 indexOf 定义的数组时 IE8 中未定义的此属性或方法
- IE8 TypeError对象没有'不支持方法的此属性
- IE8 Javascript错误:对象没有'不支持此属性或方法
- IE-“;错误:对象没有'我不支持这一行动”;
- java脚本错误对象没有'不支持此属性或方法
- 对象没有'在JavaScript中不支持此属性或方法
- 对象不支持此属性或方法 Rails Windows 64 位
- IE9:对象不支持属性“is”
- Jquery说该对象不支持“每个”