Javascript ';这';继承时内部原型方法未按预期解析

Javascript ';这';继承时内部原型方法未按预期解析,javascript,node.js,scope,prototype,this,Javascript,Node.js,Scope,Prototype,This,我有一个构造函数,它是node.js中模块()的扩展此扩展构造函数在原型中定义了另外两个方法,**方法B通过'This'关键字**调用方法A。此构造函数用于初始化另一个对象中的属性,当我从该属性调用方法B时,会出现类似“main object”没有方法a的错误: TypeError: Object function (){ superEvalidatorVar.method_B(); } has no method 'method_B' at Object.<anonymous>

我有一个构造函数,它是node.js中模块()的扩展此扩展构造函数在原型中定义了另外两个方法,**方法B通过'This'关键字**调用方法A。此构造函数用于初始化另一个对象中的属性,当我从该属性调用方法B时,会出现类似“main object”没有方法a的错误:

TypeError: Object function (){ superEvalidatorVar.method_B(); } 
has no method 'method_B' at Object.<anonymous> 
**扩展构造函数的方法_B中的“this”关键字解析为mainObject,为什么

  • 给我设置变量;在全局级别,在PropertyObject构造函数中将其设置为“this”,然后在方法B内部使用“me”而不是“this”
  • 在方法B中,使用“PropertyObject.prototype”而不是“this”
  • 但我想知道是否有什么不好的事情发生了,我不明白超级逃逸者的这种行为

    编辑:

    所有这些的真正目的是重写猫鼬模式的“验证”方法:

    PostSchema.methods.validate = function(cb){
    
        // 'this' will have the value of the object (model) from which the function will be called
        evPost.validate_super(this, cb);
    
    };
    

    问题不是由于
    与ev完全一致

    问题是您首先使用
    ev.\uuu proto\uu==={}
    创建ev,然后重新分配
    SuperEValidator.prototype
    ,这会断开与
    ev.\uu proto\uu
    SuperEValidator.prototype
    的链接

    请注意,这里是SuperEValidator的函数声明,而不是prototype define的函数声明。因此,只需将主要对象移到底部即可解决问题

    var mainObject = {
        propertyObject: new PropertyObject(),
        ev: new SuperEValidator() // at the moment, ev.__proto__ === {}
    };
    ...
    // SuperEValidator.prototype has been assigned to another reference while ev.__proto__ is still {}
    SuperEValidator.prototype = Object.create(EValidator.prototype); 
    

    请看-一切都与调用上下文有关,方法不是“对象绑定”的。谢谢,我已经讲过了,但我仍然不明白这是真的,但实际上我的问题是更棘手的。我编辑帖子来显示它。
    调用SuperEvalidator
    是一个函数,为什么要在它上面附加
    .method_B()
    ?嗯,这不是真正的代码,我只是试图将所有内容抽象到上面发布的代码中,基本上我想重写一个方法。我编辑来表达它。嗯,实际上这和我说的是同一个问题<代码>var superEvalidatorVar=新的SuperEValidator()在重新分配
    SuperEValidator.prototype
    之前执行此操作。重新分配原型后,需要稍后创建SuperEValidator对象。
    mainObject.callingSuperEValidator.method_B()应该是
    main对象。调用SuperEvalidator()
    。。。。。。
    var mainObject = {
        propertyObject: new PropertyObject(),
        ev: new SuperEValidator() // at the moment, ev.__proto__ === {}
    };
    ...
    // SuperEValidator.prototype has been assigned to another reference while ev.__proto__ is still {}
    SuperEValidator.prototype = Object.create(EValidator.prototype);