0

I've work with some framework & even wrote some libraries for my own purpose. I now working at implementation of an AngularJs router... And looked again at DI of angular:

  1. [...String, Function]
  2. function.$inject

For long I've been using the first syntax. Now while testing my router, I wanted to see the behaviour if it differs for both syntaxes and how to handle this, but...

First Hand

module.controller(function SampleController()
{
});
// Since it's and `invokelater...` function which is called right away,
SampleController.$inject = [/** my component dependencies **/]

See my face when I faced:

ReferenceError: SampleController is not defined

The Other Hand

I consider it unclean to write:

function SampleController()
{
}
SampleController.$inject = [];
moddule.$inject = [];

So Finally

I know it won't work. Why? - That's my question.

Why?

We have been taught that module, class, method/functions, some for loop, if...else create new scope.

Never have I read something like:

function's parameters are evaluated in their own scope

Please Tell Me

Thanks!

3
  • Are you somehow expecting angular to override Javascript behavior and lift your parameter outside of the function body? Commented Apr 29, 2016 at 15:10
  • mccainz - no at all, just want to made the reason clear Commented Apr 29, 2016 at 15:13
  • tushar - not the same issues are discussed there Commented Apr 29, 2016 at 15:16

1 Answer 1

3

Named function expressions only create a matching variable in their own scope (which is useful for calling the function recursively).

They create nothing in the scope of the function that holds them.

When you pass it as a function argument, then the variable you pass it into is scoped to the function call. i.e. function function_you_pass_the_function_to(someFunction) { } scopes the someFunction variable to itself.

Sign up to request clarification or add additional context in comments.

6 Comments

Please quentin, do you have any link to sustain you words?
quentin, please, would you quote to me that sentence from the spec that led to your conclusion. I'm reading but not getting that specific point - OR let's say not sure to get it
The Identifier in a FunctionExpression can be referenced from inside the FunctionExpression's FunctionBody to allow the function to call itself recursively. However, unlike in a FunctionDeclaration, the Identifier in a FunctionExpression cannot be referenced from and does not affect the scope enclosing the FunctionExpression.
Than You Quentin! Yet tell me: Is there a difference between FunctionExpression and FunctionDeclaration except Identifier is opt(ional)? ¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨ Then how this code prompt an alert box? function hello(friend) { alert('Hello ' + friend); } hello('Quentin'); since I'm referring its identifier from outside¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨ expect there is a hidden sentence somewhere stating something like functions as parameters are FunctionExpression
"Is there a difference between…" — Yes, there are lots of differences.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.