简体   繁体   English

如何调用与类中方法同名的构造函数原型的基方法?

[英]How can I call a base method of the constructor's prototype with the same name as a method in the class?

Given that I have:鉴于我有:

var PersonClass = function () {
    function Person(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;

        this.name = function(){
            return this.firstName + " " + this.lastName;
        };

        this.setName = function(a,b){
            this.firstName = a;
            this.lastName = b;
            return this.firstName + " " + this.lastName;
        }
    }

    Person.prototype.name = function () {
        return "Proto_:"+this.firstName + " " + this.lastName;
    };

    Person.prototype.whoAreYou = function () {
        return "Hi i'm " + this.name();
    };

    Person.prototype.setName2 = function(a,b){
        this.firstName = a;
        this.lastName = b;
    };

    return Person;
}(); //PersonClass

And I create an instance: Me = new PersonClass("Steve","Benj");我创建了一个实例: Me = new PersonClass("Steve","Benj"); me.name() // returns Steve Benj. me.name() // 返回 Steve Benj。 What will call the prototype name method and return Proto_:Steve Benj ?什么将调用原型名称方法并返回 Proto_:Steve Benj ?

The function defined in the Person constructor will overwrite/override the prototype definition that occurs below it, so there is no good way to invoke the prototyped function over the function defined in the constructor (see comments on your post for the "bad" way).在 Person 构造函数中定义的函数将覆盖/覆盖其下方出现的原型定义,因此没有好的方法可以通过构造函数中定义的函数调用原型函数(有关“坏”方式,请参阅您帖子中的评论) .

I would think about whether or not you want to use the same name for both of these functions, unless this is not your actual use case.我会考虑您是否要对这两个函数使用相同的名称,除非这不是您的实际用例。 I would say you would want to create a prototype function that is called protoName or something like that, if you want different behavior you should define different functions.我会说你会想要创建一个名为 protoName 或类似的原型函数,如果你想要不同的行为,你应该定义不同的函数。

The way you structure this, you invoked PersonClass function immediately after it's defined.按照您的结构方式,您在定义之后立即调用了PersonClass函数。

So PersonClass === Person .所以PersonClass === Person

So when you do Me = new PersonClass("Steve","Benj");所以当你做Me = new PersonClass("Steve","Benj"); , what you are really doing is Me = new Person('Steve', 'Benj'); , 你真正在做的是Me = new Person('Steve', 'Benj');

So whenever you invoke those prototype methods, you are calling the prototype methods from Person .因此,无论何时调用这些原型方法,都是从Person调用原型方法。

You can verify this by doing你可以通过这样做来验证这一点

Me.constructor // => Person

The relation between object and its prototype lies through constructor property.对象与其原型之间的关系在于constructor属性。
In your case the counstructor is Person function object.在您的情况下, counstructor函数是Person函数对象。
Person has direct coupling with its prototype . Person与其prototype直接耦合。
The last thing is to call a prototype method over a certain object to bind this keyword properly - and this can be achieved with Function.prototype.call() approach:最后一件事是在某个对象上调用原型方法以正确绑定this关键字 - 这可以通过Function.prototype.call()方法实现:

console.log(Me.constructor.prototype.name.call(Me));

The output will be:输出将是:

Proto_:Steve Benj

The alternative approach is using Object.getPrototypeOf(x) function which would give the same result:另一种方法是使用Object.getPrototypeOf(x)函数,它会给出相同的结果:

Object.getPrototypeOf(Me).name.call(Me)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM