javascript - what the method this.method.bind(this) does? -
this question has answer here:
- use of javascript 'bind' method 11 answers
met code first time:
var controller = function($scope){ this._scope = $scope; } controller.$inject = ['$scope']; controller.prototype.augmentscope = function() { this._scope.a = { methoda: this.methoda.bind(this) } }
i don't understand point. explanations?
it assumes in closure scope (if none in global scope such window
) there method called methoda. again, because enclosing scope mentioned), remind , assign ti used through object well. can execute by:methoda()
or a.methoda()
edit explain closure: although javascript similar java/c++ in syntax it's quite different both of them in sense when function instantiated object, remembers scope instantiated in. highly recommend doing more casual js(if there such thing), this article. this
in js deffer depends if created inside instantiated function - aka: new myclass()
. referring literal object such as:
var myobj={a:this.b}
will not create new this
, default refer enclosing scope. if none created global object such window
in browser
Comments
Post a Comment