覆盖数组推送时调用原始方法

编程入门 行业动态 更新时间:2024-10-15 02:29:28
本文介绍了覆盖数组推送时调用原始方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试覆盖数组上的 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)

更多推荐

覆盖数组推送时调用原始方法

本文发布于:2023-08-04 09:11:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1294997.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时调   数组   原始   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!