Class.prototype.method vs this.prototype.method
Asked Answered
A

4

24

I've always seen examples of Class.prototype.method, but never instances of this.prototype.method. For example:

function Class() {
    this.prototype.method = function() {
        alert("is this allowed?");
    };
}

vs

function Class() {}
Class.prototype.method = function() {
    alert("traditional example");
};
  1. Does this.prototype exist?
  2. Is it the same as Class.prototype?
  3. What about inheritance?
Acarology answered 2/3, 2014 at 4:42 Comment(1)
Its kind of confusing if you start modifying shared members on instances. You could access prototype through this.constructor. prototype in many cases but why would you? Maybe this answer can explain a bit about prototype.https://mcmap.net/q/24597/-prototypical-inheritance-writing-up-duplicateKrys
R
22
  1. Does this.prototype exist?

    No. But, this.constructor.prototype should.

  2. Is it the same as Class(.constructor).prototype?

    It will generally have the same value (so long as this.constructor === Class), but not the same intent.

  3. What about inheritance?

    Each new Class instance will inherit from Class.prototype. So, a prototype object is good for defining shared values accessible to all instances. Then, the constructor just has to setup state that's unique to each instance.

    But, attempting to mix the 2 and set a prototype property within the constructor has a few problems, including a chicken-or-egg conflict as the method won't exist until the first instance is created:

    function Class() {
        this.constructor.prototype.method = function () {};
    }
    
    console.log(typeof Class.prototype.method); // "undefined"
    
    var a = new Class();
    
    console.log(typeof Class.prototype.method); // "function"
    

    And defeats some of the benefit of having the prototype as the method is being recreated with each additional instance:

    var method = a.method;
    
    console.log(a.method === method); // true
    
    var b = new Class();
    
    console.log(a.method === method); // false
    
Riddell answered 2/3, 2014 at 5:6 Comment(3)
in what case this.prototype will exist?Rhaetian
@Pilot When this is an instanceof Function or the constructor explicitly defines this.protoytpe = ...;. It won't exist otherwise as prototypes are properties of the constructors, not of the instance.Riddell
The first isn't really a problem. The 2nd can be fixed by checking the value of this.constructor.prototype.method before setting it.Trifle
T
4

this.prototype would refer to that instance of the class object. It wouldn't give you much benefit because of the scope.

Class.prototype is adding functionality to the Class, not the instance of it.

Trot answered 2/3, 2014 at 4:54 Comment(0)
S
3

Class is a function; this is an object. Functions have a prototype property; objects do not. There's a __proto__ property defined on objects, but that interface is deprecated. You can do something like

function Class () {
    var prototype = Object.getPrototypeOf(this);
    prototype.method = function () {};
}

inside your constructor but it's not really good practice - every time Class is instantiated, it will needlessly waste cycles overwriting method on the prototype, and in cases with more complex code, may end up wasting memory as well.

In short, there's no upside and possibly serious downsides to doing it that way.

Shovelnose answered 2/3, 2014 at 5:5 Comment(0)
F
0

You can achieve the same effect in two different ways. But in both, you must first instantiate Class.

First way

function Class () {
    var prototype = Object.getPrototypeOf(this);
    prototype.foo=100
}

new Class()

console.log(Object.getOwnPropertyNames(Class.prototype))

And second

function Class () {
    //var prototype = Object.getPrototypeOf(this);
    var prototype=this.constructor.prototype
    prototype.foo=100
}

new Class()

console.log(Object.getOwnPropertyNames(Class.prototype))

Both give output

["constructor", "foo"]

The only difference is that instead of doing this.constructor.prototype you can get it returned from Object.getPrototypeOf(this). Both returns the prototype of an instance of Class.

Forefend answered 12/10, 2022 at 1:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.