错误"对象在自定义grunt任务上没有方法'endsWith'"

Eri*_*ria 1 javascript gruntjs

我在自定义的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' ) );
    })

};
Run Code Online (Sandbox Code Playgroud)

测试

> 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
Run Code Online (Sandbox Code Playgroud)

就像grunt无法识别String.proptotype.endsWith函数一样.这是正常的吗?

编辑:我正在使用节点v0.10.4

Mik*_*uck 6

.endsWith是ES6功能,并未在Node.js v0.10.4中实现.

要使用.endsWith升级Node.js或添加polyfill:

String.prototype.endsWith = function(suffix) {
    return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
Run Code Online (Sandbox Code Playgroud)