1

this is my code

const myFunction = function(number){

    this.number = number;
    
    const foo = () => {
        return this.number;
    }
 
    myFunction.prototype.log = function () {
        console.log(foo());
    }
 
    
}

let firstInstance = new myFunction(12);
let secondInstance = new myFunction(13);

firstInstance.log() // 13
secondInstance.log() // 13

when I'm using the foo function, returns the last instance;

is there a way to using a private function and log function in the prototype of the function

  • No, there is no way to access constructor-scoped functions from a shared prototype method. You'd want to just use `this.log = foo;` instead (although given that the `.number` property is public, I don't see a reason to make `foo` private at all). – Bergi Sep 20 '20 at 10:32

0 Answers0