我在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定義為一個數(shù)組,res:[],然后,里面存放的是object對象,然后,我在調(diào)用我的自定義方法的時候,報錯提示:Uncaught TypeError: Property 'getIndexByMsg' of object [object Object],[object Object],[object Object],[object Object] is not a function;這讓我很奇怪了,vue中的數(shù)組對象,我用typeof顯示的是object?我該怎么改,才能讓vue中的數(shù)組能夠識別到我自定義的方法呢?我嘗試換一個簡單的測試,但是結果還是一樣的。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中的數(shù)組,沒法獲取我添加在數(shù)組prototype上的方法。
眼眸繁星
2018-12-12 18:19:56