在微信微型應用平臺(基於微信應用的javascript +本地混合)上開發性能評測工具,我試圖在其原型中注入代碼,例如wx.request
函數。是否可以修改定義的getter函數?
這是你將如何使用wx.request
功能:
wx.request({
url: 'test.php',
data: {
x: '' ,
y: ''
},
header: {
'content-type': 'application/json'
},
success: function(res) {
console.log(res.data)
}
})
所以爲了知道請求了多長時間拍攝,而無需手動編寫將所有的錨,我試圖注入的代碼:
var owxrequest = wx.request
wx.request = function() {
console.log('test', Date.now())
return owxrequest.apply(owxrequest, arguments)
}
這失敗了,我得到了Cannot set property "prop" of #<Object> which has only a getter
錯誤。
所以我實現了對象必須已定義類似於:
wx = {
request: get function(){
...
}
...
}
所以,我想:
var owxrequest = wx.request
Object.defineProperty(wx, 'request', {
get: function() {
console.log('test', Date.now())
return owxrequest.apply(owxrequest, arguments)
}
})
這個錯誤而失敗(request: fail parameter error: parameter.url should be String instead of Undefined
)。然後我想:
var owxrequest = wx.request
Object.defineProperty(wx, 'request', {
set: function() {
console.log('test', Date.now())
return owxrequest.apply(owxrequest, arguments)
}
})
這會不會引發錯誤,但調用wx.request()
時也沒有效果...
這就是你如何定義一個吸氣劑,我明白了! –
@AeroWang我也爲問題中提到的錯誤添加了解釋。請檢查。 – shaochuancs