1

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.name() // returns Steve Benj. What will call the prototype name method and return Proto_:Steve Benj ?

  • Similar question [here](http://stackoverflow.com/questions/560829/calling-base-method-using-javascript-prototype). I think what you are trying to do in this example is not possible, you may need to try a different approach. – minorcaseDev Jan 20 '17 at 22:10
  • `me.__proto__.name()` returns the proto-string but `__proto__` shoudn't be used - https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Object/proto – Thomas Altmann Jan 20 '17 at 22:14
  • Possible duplicate of [Calling base method using JavaScript prototype](http://stackoverflow.com/questions/560829/calling-base-method-using-javascript-prototype) – Heretic Monkey Jan 20 '17 at 22:23

3 Answers3

1

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).

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.

minorcaseDev
  • 181
  • 5
0

The way you structure this, you invoked PersonClass function immediately after it's defined.

So PersonClass === Person.

So when you do Me = new PersonClass("Steve","Benj");, what you are really doing is Me = new Person('Steve', 'Benj');

So whenever you invoke those prototype methods, you are calling the prototype methods from Person.

You can verify this by doing

Me.constructor // => Person
Edmund Lee
  • 2,514
  • 20
  • 29
0

The relation between object and its prototype lies through constructor property.
In your case the counstructor is Person function object.
Person has direct coupling with its 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:

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(Me).name.call(Me)
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
  • isn't that the same as `Me.constructor.prototype.name()`? – Thomas Altmann Jan 20 '17 at 22:15
  • @ThomasAltmann, `Me.constructor.prototype.name()` will give `Proto_:undefined undefined`. See my explanation about `.call()` – RomanPerekhrest Jan 20 '17 at 22:19
  • Oh right sorry, i just looked at the first few characters of the output and thought everything went thine, thanks – Thomas Altmann Jan 20 '17 at 22:20
  • *"The relation between object and its prototype lies through `constructor` property:"* I think this is misleading. Every object has an internal `[[Prototype]]` property that references the object's prototype. The value of the object's `constructor` doesn't matter. E.g. `var foo = {bar: 42}; var baz = Object.create(foo);`. `Object.getPrototypeOf(baz) !== baz.constructor.prototype`. – Felix Kling Jan 20 '17 at 22:27
  • @FelixKling, I meant the "evident" relation. I didn't want to push the OP to use `__proto__`(we know that it's not standard) – RomanPerekhrest Jan 20 '17 at 22:31
  • I see. Well, `Object.getPrototypeOf(x)` would be the more reliable, modern alternative. – Felix Kling Jan 20 '17 at 22:32
  • @FelixKling, ok, I would call `.constructor.prototype` an "old - good - proven" approach. But, also added `Object.getPrototypeOf(x)` as an alternative – RomanPerekhrest Jan 20 '17 at 22:38