我在自定义 grunt 任务中遇到错误。下面我发布了一个与问题相关的简单测试用例:
Gruntfile.js
module.exports = function( grunt ){
grunt.task.registerTask( 'endsWith', 'Test of string.prototype.endsWith', function(){
var value = grunt.option('value');
grunt.log.writeln( typeof value );
grunt.log.writeln( value.endsWith( 'bar' ) );
})
};
测试
> grunt endsWith --value=foobar
Running "endsWith" task
string
Warning: Object foobar has no method 'endsWith' Use --force to continue.
Aborted due to warnings.
Execution Time (2016-02-12 16:15:19 UTC)
Total 12ms
这就像 grunt 不识别 String.proptotype.endsWith 函数一样。这正常吗?
编辑:我正在使用节点 v0.10.4
最佳答案
.endsWith
is an ES6 feature并且未在 Node.js v0.10.4 中实现。
要使用 .endsWith
升级 Node.js 或 add in a polyfill :
String.prototype.endsWith = function(suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
关于javascript - 错误 "Object has no method ' endsWith'"在自定义 grunt 任务上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35367357/