函数拦截
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function func() {
console.log('原始功能')
}
let _tempFn = func
func = function () {
_tempFn()
console.log('新的扩展功能')
}
func();
</script>
</body>
</html>
数组拦截
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let ARRAY_METHOD = [
'push',
'shift',
'unshif',
'revers',
'sort',
'splice',
]
let arr = []
let array_methods = Object.create( Array.prototype)
ARRAY_METHOD.forEach( method => {
array_methods[method] = function () {
console.log('调用拦截')
let res = Array.prototype[ method ].apply( this , arguments)
return res
}
} )
arr.__proto__ = array_methods
</script>
</body>
</html>