![](/img/trans.png)
[英]Grunt: Warning: Object #<Object> has no method 'indexOf'
[英]Error “Object has no method 'endsWith'” on custom grunt task
我在執行自定義任務時遇到錯誤。 下面我發布了一個與問題有關的簡單測試用例:
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
是ES6的功能 ,Node.js v0.10.4中未實現。
使用.endsWith
升級Node.js或添加 .endsWith
:
String.prototype.endsWith = function(suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
如果您使用的節點版本較舊,則可以使用String.match方法。
更換
grunt.log.writeln(value.endsWith('bar'));
與
grunt.log.writeln( value.match("bar$") );
完整的代碼
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.match("bar$") );
})
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.