Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
重写javascript中的函数_Javascript - Fatal编程技术网

重写javascript中的函数

重写javascript中的函数,javascript,Javascript,javascript库中有一个函数我想为我自己的用例重写,我想使用函数中的大部分代码,但添加一些额外的功能。在我的控制台中,我可以通过执行,somelibrary.CharCounter.prototype.count来访问该函数此函数的参数为text 我试过以下方法 somelibrary.CharCounter.prototype.count = (function(_super, text) { return function(_super, apply) { cons

javascript库中有一个函数我想为我自己的用例重写,我想使用函数中的大部分代码,但添加一些额外的功能。在我的控制台中,我可以通过执行,
somelibrary.CharCounter.prototype.count来访问该函数
此函数的参数为text

我试过以下方法

somelibrary.CharCounter.prototype.count = (function(_super, text) {
   return function(_super, apply) {
       console.log("overwriting", text);
       return _super.apply(this, arguments);
   };
})(somelibrary.CharCounter.prototype.count)
在上面,我得到了console.log,正如我所期望的,但我也得到了这个错误

未捕获类型错误:\ u super.apply不是函数

很明显,我做错了什么,我只想验证函数,使其返回与原始方法不同的内容。


只需将
\u super
传递给外部函数,并使内部函数接受“super”参数:

Array.prototype.slice=(函数(_super){
返回函数(x,y){
控制台日志(“覆盖”,x,y);
return _super.apply(这是参数);
};
})(Array.prototype.slice);
console.log([1,2,3,4,5,6].slice(1,3))

只需将
\u super
传递给外部函数,并使内部函数接受“super”参数:

Array.prototype.slice=(函数(_super){
返回函数(x,y){
控制台日志(“覆盖”,x,y);
return _super.apply(这是参数);
};
})(Array.prototype.slice);

console.log([1,2,3,4,5,6].slice(1,3))
可能更具可读性的解决方案:

somelibrary.CharCounter.prototype.foo = (function () {
  var _super = somelibrary.CharCounter.prototype.foo;
  return function (text) {
    console.log("overwriting", text);
    return _super.apply(this, arguments);
  }
})();
还可以定义泛型
extendedfunction()
函数来覆盖函数:

function extendFunction(superFunc, overridingFunc) {
  return function () {
    overridingFunc.call(this, superFunc, arguments);
  }
}

somelibrary.CharCounter.prototype.foo = extendFunction(
  somelibrary.CharCounter.prototype.foo,
  function (superFunc, args) {
    console.log("overwriting", args);
    return superFunc.apply(this, args);
  }
);

可能更具可读性的解决方案:

somelibrary.CharCounter.prototype.foo = (function () {
  var _super = somelibrary.CharCounter.prototype.foo;
  return function (text) {
    console.log("overwriting", text);
    return _super.apply(this, arguments);
  }
})();
还可以定义泛型
extendedfunction()
函数来覆盖函数:

function extendFunction(superFunc, overridingFunc) {
  return function () {
    overridingFunc.call(this, superFunc, arguments);
  }
}

somelibrary.CharCounter.prototype.foo = extendFunction(
  somelibrary.CharCounter.prototype.foo,
  function (superFunc, args) {
    console.log("overwriting", args);
    return superFunc.apply(this, args);
  }
);
你可以用a来做这个。下面是一个关于代理
Math.sin的演示:

Math.sin=新代理(Math.sin{
应用:函数(原始、此参数、参数){
log(`Executing${original.name}(${args.join()})`);
返回原件。应用(thisArg,args);
}
});
console.log(Math.sin(2))您可以为此使用。下面是一个关于代理
Math.sin的演示:

Math.sin=新代理(Math.sin{
应用:函数(原始、此参数、参数){
log(`Executing${original.name}(${args.join()})`);
返回原件。应用(thisArg,args);
}
});
console.log(Math.sin(2))固定版本:固定版本: