3

Met the 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 really don't understand what is the point. Any explanations?

nik
  • 303
  • 3
  • 15
  • 1
    What exactly is is that you don't understand? `.bind(this)`? Did you read the [**documentation**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind) about `.bind`? What is still unclear after you read it? – Felix Kling Apr 14 '15 at 17:58
  • it makes `methodA` not be a method of `a`, but rather a stand-alone function – dandavis Apr 14 '15 at 18:02
  • an object called `a` is created in which there is 1 item therein, a function, `methodA` which is defined in the scope of `a`. the bind is related to passing this to be the this of the function. so you are looking at something a bit more like: `this.methodA = function(){}; var a = {methodA: this.methodA.bind(this)};` – Fallenreaper Apr 14 '15 at 18:04
  • @Fallenreaper: *"`methodA` which is defined in the scope of `a`"* I guess you are just using the term "scope" wrongly. An object does not create or "is" scope. In ES5 only functions create scope. – Felix Kling Apr 14 '15 at 18:05
  • true, that way does not. My bad. There is no scope inside of something like that. Im used to making Objects with prototypes which have their own scopes vs a map. – Fallenreaper Apr 14 '15 at 18:07
  • @FelixKling, this question is less about bind and more about what 'this' means. What 'this' means will change depending or where you call it in JS. Specifically in Angular it will change depending on how the developer bound the controller to the scope context. I do not thing it is a duplicated of what you had mentioned. Granted, more rigorous reading of documentation of both JS and Angular can answer this question. – Gabriel Kohen Apr 14 '15 at 18:41
  • @GabrielKohen: What makes you think the question is specifically about `this`? – Felix Kling Apr 14 '15 at 18:42
  • @FelixKling, the this you're binding to is probably the controller instantiated by AngularJS. I have never seen someone use Controllers like this before but what it seems to do is do use this to bind the method of the object to the scope (which is really $scope passed to the controller), which will allow it to be used from an Angular directive if you had decided not to use the controllerAs syntax. I think this is more of an Angular question and the code might be misusing how Angular +1.3 is recommended to be used. – Gabriel Kohen Apr 14 '15 at 18:51
  • @GabrielKohen: You may be right. That's why my very first comment asks for clarification of the question. Alas, the OP hasn't given any. – Felix Kling Apr 14 '15 at 18:53

1 Answers1

0

It assumes that in the closure scope (If none it will be in the global scope such as window) there is a method called methodA. Then again, because the this is really the enclosing scope mentioned), it will remind it and assign it ti be used be through object a as well. So you can execute it by:methodA() or a.methodA()

Edit to explain closure: Although Javascript is very similar to Java/C++ in syntax it's quite different than both of them in the sense that when a function is instantiated as an object, it remembers the scope it was instantiated in. I would highly recommend anyone doing more than the casual JS(if there is such a thing), to look into this article. The this in JS will deffer depends if it was created inside an instantiated function - AKA: new MyClass(). Referring to a literal object such as:

var myObj={a:this.b}

will not create a new this and will by default refer to enclosing scope. If none was created it will be the global object such as window in a browser

Gabriel Kohen
  • 4,166
  • 4
  • 31
  • 46
  • *"It assumes that in the closure scope (If none it will be in the global scope such as window)"* What closure? It actually assumes that the value of `this` has a property `methodA`. – Felix Kling Apr 14 '15 at 18:03
  • so, we are giving a reference to the local scope for the outside access(from the $scope)? – nik Apr 14 '15 at 18:16
  • OK. I assume you might be using AngularJS by your use of $scope, right? Is there a snippet of code you can share on Plunkr or FiddleJS? – Gabriel Kohen Apr 14 '15 at 18:18
  • @johndoe: No, it has nothing to do with scope. The value of `this` usually depends on how the function is *called*. `.bind` ensures that `this` will always refer to the passed value, no matter how the function was called. Learn mode about `this`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this – Felix Kling Apr 14 '15 at 18:31
  • You are mixing closure, scope and `this` and while there is some relation, `.bind` has nothing to do with closures or scope. – Felix Kling Apr 14 '15 at 18:32
  • @Felix Kling, and jonedoe. It does not have to do with $scope but it has to do with Javascript scope which determines what 'this' means in each scope as mentioned . – Gabriel Kohen Apr 14 '15 at 18:35
  • @GabrielKohen: No, I'm specifically talking about JS scope. *"but it has to do with Javascript scope which determines what 'this' means in each scope as mentioned "* That's **coincidentally** true because in ES5 only functions create scope. However, in ES6, blocks and classes also create scope, but they have the same `this` value as the parent environment. As I said, there certainly is a relation between scope and `this`, but to say that `this` *is* scope is completely wrong. – Felix Kling Apr 14 '15 at 18:37