Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.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,我试图装饰一些物品,例如: Object.prototype.someFunc = function(x) { if (x == 1) { return x; } return super.someFunc() // <-- ? } Object.prototype.someFunc=函数(x){ 如果(x==1){ 返回x; } 返回super.someFunc()//您需要进行备份。例如: var __superFunc = Object.protot

我试图装饰一些物品,例如:

Object.prototype.someFunc = function(x) {
  if (x == 1) {
      return x;
  } 

  return super.someFunc() // <-- ? 
}
Object.prototype.someFunc=函数(x){
如果(x==1){
返回x;
} 

返回super.someFunc()//您需要进行备份。例如:

var __superFunc = Object.prototype.someFunc;
Object.prototype.someFunc = function(x) {
  if (x == 1) {
      return x;
  }

  return __superFunc.call(this, x);
}

必须重写整个代码;您还需要为父级指定函数的行为(以防止递归调用)…下面是使用call方法对解决方案的提示:

Object.prototype.someFunc.call(this, x);

在javascript中,没有真正的本机继承实现

您可以利用继承:

function Parent() { }

Parent.prototype.someFunc = function(x) {
    var result = 0;
    if (x == 1) {
        result = 1;
    }
    return result;
}

function Child() { }

Child.prototype = Object.create(Parent.prototype); //inheritance
Child.prototype.constructor = Child; //enforce the constructor to be Child instead of Parent

Child.prototype.someFunc = function(x) {
    return Parent.prototype.someFunc.call(this, x); //call your Parent prototype someFunc passing your current instance
}

var child = new Child();
console.log(child.someFunc(1)); //1
console.log(child.someFunc(2)); //0

避免扩展本机原型。首先,您通常不应该添加到本机对象的原型中,因为这可能会导致问题。其次,您必须将旧函数存储在临时变量中。可能重复的函数可能会更清楚您试图在这里做什么,因为这是访问obje上函数的一种特殊方式ct的父项。完全取决于你所说的“覆盖”是什么意思。你要替换它吗?你在原型链中跟踪它吗?这就是为什么你需要给出一个实际的、完整的代码演示。事实上,你的问题是不清楚的。
super
是一个保留字。x是否也需要传递给u superfunc?\uu superfunc(x)我怀疑第一个会起作用。
this.prototype.someFunc
不存在。你们两个,没有看到注释吗?没有。因为1)这是一个你永远不应该使用的私事,2)someFunc
是新函数。你对这个方法进行了静态调用。这意味着在函数中,如果你使用
this
,你会没有正确获取您正在处理的实例。应该使用call/apply/bind。谢谢!
Object.someFunc
实际上不存在。
(新对象()).someFunc
do.or
Object.prototype.someFunc
。所以这里还有一个问题。当您执行
Object.prototype.someFunc=function(x){…}
Object.prototype.someFunc的值被覆盖。您有权在调用之前捕获上一个值。您的答案将导致无限循环和最大堆栈大小错误。我不理解您的注释。此代码
Object.prototype.someFunc=function(x){…}
完成一次,可能是在页面加载中。然后在运行时,当对
对象的某个实例调用
someFunc
时,我们可以执行
Object.prototype.someFunc.call(这个,x)所以,我不明白你在哪里能看到它们overriding@ikken:将代码放入原始代码中并进行测试。您将看到有一个没有转义条件的递归调用。因此OP询问如何访问覆盖它的函数中的原始(或超级)函数。您将需要以两个函数结束。
function Parent() { }

Parent.prototype.someFunc = function(x) {
    var result = 0;
    if (x == 1) {
        result = 1;
    }
    return result;
}

function Child() { }

Child.prototype = Object.create(Parent.prototype); //inheritance
Child.prototype.constructor = Child; //enforce the constructor to be Child instead of Parent

Child.prototype.someFunc = function(x) {
    return Parent.prototype.someFunc.call(this, x); //call your Parent prototype someFunc passing your current instance
}

var child = new Child();
console.log(child.someFunc(1)); //1
console.log(child.someFunc(2)); //0