javascript - 错误 "Object has no method ' endsWith'"在自定义 grunt 任务上

标签 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' ) );
    })

};

测试

> 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/

相关文章:

javascript - SVG - IE11-10 变换旋转似乎不起作用

javascript - Grunt uglify - 替换原始文件

Git:存储但不提取 LESS 文件

javascript - GruntJS 链表

javascript - 将数组从 MVC 传递到 javascript?

javascript - 将页面添加到浏览器历史记录中,而无需实际打开该页面

javascript - 如何检测字符串 `article/page/inc/3/4` 在 `inc` 的最后第三个索引中有 `/` ?

javascript - 带有 .wait 和 .animate 的 jQuery 顺序 CSS 动画

javascript - 加载 "Gruntfile.js"任务语法错误

javascript - Grunt -contrib-concat : how to use option "process"