覆盖数组推送时调用原始方法 [英] Calling original method when overriding array push
本文介绍了覆盖数组推送时调用原始方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试覆盖数组上的 push
。我需要能够在覆盖之前调用原始的 push
方法,以便可以将元素压入数组。
I'm attempting to override push
on an array. I need to be able to call the original push
method before overriding so I can push the element onto the array.
下面是我的代码。我在 value
方法中添加了 list.push
。我知道这是错误的,但是我添加了它以显示我要执行的操作。
Below is my code. I've added list.push
within the value
method. I know this is wrong, but I added it to show what I am trying to do.
let list = [
'One',
'Two',
'Three'
]
Object.defineProperty(list, 'push', {
value: function(el) {
list.push(el) // I know this is wrong. Example of what I am trying to do.
}
})
推荐答案
以当前对象作为上下文调用原始原型实现:
Invoke the original prototype implementation with your current object as context:
Array.prototype.push.call(this, el)
这篇关于覆盖数组推送时调用原始方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文