javascript - what the method this.method.bind(this) does? -


this question has answer here:

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

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

node.js - How to abort query on demand using Neo4j drivers -