Skip to content

console.log in prototypes #38272

Answered by aduh95
Martii asked this question in General
Apr 17, 2021 · 1 comments · 4 replies
Discussion options

You must be logged in to vote

Do you have something specific in mind? Depending on what myobject is, it should work just fine.

  • if myobject is a class:
    class myobject {}
    myobject.prototype.method = function () { console.log('foo?'); }
    new myobject().method(); // outputs `foo?` in the console.
  • if myobject is an instance of a class, you need to reach the prototype of its constructor class:
    class SomeClass {}
    const myobject = new SomeClass();
    myobject.constructor.prototype.method = function () { console.log('foo?'); }
    myobject.method(); // outputs `foo?` in the console.
    Alternatively you can set a method specific to the instance:
    class SomeClass {}
    const myobject = new SomeClass();
    myobject.method = function () { console.

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@Martii
Comment options

@aduh95
Comment options

@Martii
Comment options

@Martii
Comment options

Answer selected by Martii
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants