-
Notifications
You must be signed in to change notification settings - Fork 27.4k
docs(guide/Controllers): I think that it is better to say that a controller is defined using a JavaScript constructor function as we define a class through a constructor. #11888
Conversation
…roller is defined using a JavaScript constructor function as we define a class through a constructor. A controller is a instantiated object created out of the constructor function. So, I feel we should not call a Controller a constructor function.
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project, in which case you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed, please reply here (e.g.
|
The cla has been signed. Thanks On Sun, May 17, 2015 at 11:28 PM, googlebot [email protected]
|
CLAs look good, thanks! |
I am not convinced that this makes any difference. Constructor functions in JavaScript are not specific to classes. They are simply functions that create and initialise objects, which is exactly what a controller's constructor function is used for. |
I will try to make my point more clear. The doc says "In Angular, a Controller is a JavaScript constructor function that is used to augment the Angular Scope." However, I believe that a Controller is an instance or Javascript object that has been created/configured using the constructor and thus is not a JavaScript constructor function itself. Also I believe that the object is created using the 'new' keyword which is referred by 'this' in the constructor function. So strictly speaking, the object is just initialized/configured by the constructor function. So, in my opinion, it would be better to say that "In Angular, a Controller is defined using a JavaScript constructor function that is used to augment the Angular Scope." |
Let's say you have a function that is going to "define" a controller: The it is true that we do not exactly call
where Then inside the function instantiate(Type, locals, serviceName) {
// Check if Type is annotated and use just the given function at n-1 as parameter
// e.g. someModule.factory('greeter', ['$window', function(renamed$window) {}]);
// Object creation: http://jsperf.com/create-constructor/2
var instance = Object.create((isArray(Type) ? Type[Type.length - 1] : Type).prototype || null);
var returnedValue = invoke(Type, instance, locals, serviceName);
return isObject(returnedValue) || isFunction(returnedValue) ? returnedValue : instance;
} You can see that we are basically simulating Now are you saying that because we are only simulating In any case, I am still of the opinion, unless you can convince me otherwise, that there is no semantic or practical value in distinguishing in the way that you suggest. |
I will try to be even more elaborate. The doc says "In Angular, a Controller is a JavaScript constructor ". After reading the above line in quotes, a question comes to my mind. Is the controller really a constructor and the answer to this is 'NO'(at least for me). To me, controller is something which controls some part of our logic and hence it is not a constructor function according to me but a outcome(an instance) of running of constructor function. Thus I still think it is better to say that "a controller is defined using a java script constructor function". Hope this will convey my thought better. Thanks for taking me inside the source of Angular, but I believe that from end user perspective, my suggestion can be helpful. |
Oh right! I get it. OK. Sounds reasonable. |
… constructor function A controller is a instantiated object created **from** a constructor function. It was not accurate to describe a Controller **as** a constructor function. Closes #11888
… constructor function A controller is a instantiated object created **from** a constructor function. It was not accurate to describe a Controller **as** a constructor function. Closes angular#11888
… constructor function A controller is a instantiated object created **from** a constructor function. It was not accurate to describe a Controller **as** a constructor function. Closes angular#11888
A controller is a instantiated object created out of the constructor function. So, I feel we should not call a Controller a constructor function.