我在Array.prototype上添加了一个自定义的查询方法,
Array.prototype.getIndexByMsg = function(judgeFun) {
for(var i = 0, len = this.length; i < len; i++) {
if(judgeFun(this[i])) {
return i;
}
}
return -1;
}
然后,在vue中,res定义为一个数组,res:[],然后,里面存放的是object对象,然后,我在调用我的自定义方法的时候,报错提示:Uncaught TypeError: Property 'getIndexByMsg' of object [object Object],[object Object],[object Object],[object Object] is not a function;
这让我很奇怪了,vue
中的数组对象,我用typeof
显示的是object
?
我该怎么改,才能让vue中的数组能够识别到我自定义的方法呢?
我尝试换一个简单的测试,但是结果还是一样的。
Array.prototype.testQ = function(){
console.log(this[0]);}
var testAry = [1,2,3,4];
testAry.testQ();
能正常打印1,我再试一下放在vue中使用。
created: function() {
console.log(this.res instanceof Array);
this.res.testQ();},
显示instanceof显示的是true,但是,testQ则报错,“TypeError: Object has no method 'testQ'”;
vue
中扩展原生方法的,没有任何问题。typeof
得到的结果肯定是object
,这和有没有vue
没啥关系。