javascript - this keyword in a class method -
var mc = function(map){ var init = function(imap){ alert("init " + + " with"); } init(map); }; var m = new mc({});
why getting value of [object window]? window object??
yes! because init
variable of mc
, share scope (which global scope, window
object).
however. if changed following:
var mc = function(map){ this.init = function(imap){ alert("init " + + " with"); } this.init(map); }; var m = new mc({});
then this
inside init
reference instance.
Comments
Post a Comment