javascript - Reference to this within prototype function -
javascript - Reference to this within prototype function -
i'm using form hide private functions of class: this not expect.
function person(){} person.prototype = (function(){ console.log(this); // --> window, function privatefunc(from){ // private func loses 'this' if publicfunc had console.log('private', from, this); } function publicfunc(from){ // publicfunc has when called directly, not timeout console.log('public', from, this); privatefunc(from); } homecoming { publicfunc: publicfunc }; })/*.bind(???).*/(); var man = new person(); man.publicfunc('direct'); window.settimeout(man.publicfunc, 1, 'timeout'); i solve binding functions manually such:
privatefunc.call(this, from); window.settimeout(man.publicfunc.bind(man), 1, 'timeout'); but not calling each private function call. there approach?
javascript prototype prototype-programming
Comments
Post a Comment