如何在不出现 JSHint 问题的情况下扩展 javascript 字符串?
How can I extend javascript String without getting a JSHint Issue?
您好,我正在使用原型扩展字符串,但 JSHint 抛出警告:
Extending prototype of native object: 'String'.
我应该如何解决这个问题?我认为警告是因为这种方法被认为是不好的做法。
String.prototype.decodeHTML = function() {
return $('<div>', {html: '' + this}).html();
};
非常感谢任何帮助,谢谢。
更新这确实有效:
这是为那些需要它的人提供的解决方案。
Object.defineProperty(String.prototype, "decodeHTML", {
value: function () {
return $('<div>', {html: '' + this}).html();
}
});
参见 JSLint 文档:https://jslinterrors.com/extending-prototype-of-native-object。你应该使用
Object.defineProperty(String.prototype, "decodeHTML", ...)
相反。
只有在 freeze
option 启用时才会发出相关警告。您可以使用指令注释为相关块(或文件)禁用该选项:
/*jshint freeze: false */
String.prototype.decodeHTML = function() {
return $('<div>', {html: '' + this}).html();
};
您好,我正在使用原型扩展字符串,但 JSHint 抛出警告:
Extending prototype of native object: 'String'.
我应该如何解决这个问题?我认为警告是因为这种方法被认为是不好的做法。
String.prototype.decodeHTML = function() {
return $('<div>', {html: '' + this}).html();
};
非常感谢任何帮助,谢谢。
更新这确实有效:
这是为那些需要它的人提供的解决方案。
Object.defineProperty(String.prototype, "decodeHTML", {
value: function () {
return $('<div>', {html: '' + this}).html();
}
});
参见 JSLint 文档:https://jslinterrors.com/extending-prototype-of-native-object。你应该使用
Object.defineProperty(String.prototype, "decodeHTML", ...)
相反。
只有在 freeze
option 启用时才会发出相关警告。您可以使用指令注释为相关块(或文件)禁用该选项:
/*jshint freeze: false */
String.prototype.decodeHTML = function() {
return $('<div>', {html: '' + this}).html();
};