Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

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

Closed
wants to merge 1 commit into from

Conversation

niteshthakur
Copy link
Contributor

A controller is a instantiated object created out of the constructor function. So, I feel we should not call a Controller a constructor function.

Review on Reviewable

…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.
@googlebot
Copy link

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. I signed it!) and we'll verify. Thanks.


  • If you've already signed a CLA, it's possible we don't have your GitHub username or you're using a different email address. Check your existing CLA data and verify that your email is set on your git commits.
  • If you signed the CLA as a corporation, please let us know the company's name.

@niteshthakur
Copy link
Contributor Author

The cla has been signed.

Thanks

On Sun, May 17, 2015 at 11:28 PM, googlebot [email protected]
wrote:

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).

[image: 📝] Please visit https://cla.developers.google.com/
https://cla.developers.google.com/ to sign.

Once you've signed, please reply here (e.g. I signed it!) and we'll

verify. Thanks.


Reply to this email directly or view it on GitHub
#11888 (comment).

@googlebot
Copy link

CLAs look good, thanks!

@petebacondarwin
Copy link
Contributor

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.

@petebacondarwin petebacondarwin added this to the Ice Box milestone Jun 30, 2015
@petebacondarwin petebacondarwin self-assigned this Jun 30, 2015
@niteshthakur
Copy link
Contributor Author

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."

@petebacondarwin
Copy link
Contributor

Let's say you have a function that is going to "define" a controller: function MyController($scope, ...).

The it is true that we do not exactly call new MyController($scope, ...) to "construct" the controller object. This is because we must apply the injectables to the parameters when creating the object. Inside $controller we have the following line of code:

instance = $injector.instantiate(expression, locals, constructor);

where constructor is exactly the function that we are discussing (e.g. MyController).

Then inside the $injector the instantiate method looks like this:

    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 new Constructor(...) using a combination of Object.create and $injector.invoke.

Now are you saying that because we are only simulating new Constructor(...) that the function that we provide when registering a controller is not really a constructor? Or something else?

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.

@niteshthakur
Copy link
Contributor Author

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.

@petebacondarwin
Copy link
Contributor

Oh right! I get it. OK. Sounds reasonable.

petebacondarwin pushed a commit that referenced this pull request Jul 13, 2015
… 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
netman92 pushed a commit to netman92/angular.js that referenced this pull request Aug 8, 2015
… 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
ggershoni pushed a commit to ggershoni/angular.js that referenced this pull request Sep 29, 2015
… 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
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants