The difference is that functionOne
is a function expression and so only defined when that line is reached, whereas functionTwo
is a function declaration and is defined as soon as its surrounding function or script is executed (due to hoisting).
For example, a function expression:
// TypeError: functionOne is not a function
functionOne();
var functionOne = function() {
console.log("Hello!");
};
And, a function declaration:
// Outputs: "Hello!"
functionTwo();
function functionTwo() {
console.log("Hello!");
}
Historically, function declarations defined within blocks were handled inconsistently between browsers. Strict mode (introduced in ES5) resolved this by scoping function declarations to their enclosing block.
'use strict';
{ // note this block!
function functionThree() {
console.log("Hello!");
}
}
functionThree(); // ReferenceError
It's interesting in JavaScript. Consider this example:
function changeStuff(a, b, c)
{
a = a * 10;
b.item = "changed";
c = {item: "changed"};
}
var num = 10;
var obj1 = {item: "unchanged"};
var obj2 = {item: "unchanged"};
changeStuff(num, obj1, obj2);
console.log(num);
console.log(obj1.item);
console.log(obj2.item);
This produces the output:
10
changed
unchanged
- If
obj1
was not a reference at all, then changing obj1.item
would have no effect on the obj1
outside of the function.
- If the argument was a proper reference, then everything would have changed.
num
would be 100
, and obj2.item
would read "changed"
. Instead, num
stays 10
and obj2.item
remains "unchanged
".
Instead, the situation is that the item passed in is passed by value. But the item that is passed by value is itself a reference.
Technically, this is called call-by-sharing.
In practical terms, this means that if you change the parameter itself (as with num
and obj2
), that won't affect the item that was fed into the parameter. But if you change the internals of the parameter, that will propagate back up (as with obj1
).
Edited for clarification:
JavaScript is pass-by-value. Passing-by-reference and passing a reference are two different things. Pass-by refers to the mechanism for passing a parameter to a function. It's either the value of the variable (pass-by-value) or a pointer (reference) to the location of the variable (pass-by-reference). There's no mechanism within JavaScript to do the latter. It's not as complicated or nuanced as some of these answers try to make it.
Think of a variable as a box that contains information. When you pass it by value, you're just passing a new box with a copy of the information contained in the box. If you change that information within the new box in the function, the original value in the original box is unchanged.
Passing by reference is passing the actual box. If you change what's inside, it will be changed even after the function exits.
In the case of objects, think of the information contained within the box as a map to the locations of other boxes. You can change the contents of those boxes which will be permanent changes after the function exits, but you can't change the contents of the original map.
Assigning a new object to c just put a new map into the new box that is only local to the function. This is also what happened with a.
Best Answer
First of all, parent-child scope relation does matter. You have two possibilities to emit some event:
$broadcast
-- dispatches the event downwards to all child scopes,$emit
-- dispatches the event upwards through the scope hierarchy.I don't know anything about your controllers (scopes) relation, but there are several options:
If scope of
firstCtrl
is parent of thesecondCtrl
scope, your code should work by replacing$emit
by$broadcast
infirstCtrl
:In case there is no parent-child relation between your scopes you can inject
$rootScope
into the controller and broadcast the event to all child scopes (i.e. alsosecondCtrl
).Finally, when you need to dispatch the event from child controller to scopes upwards you can use
$scope.$emit
. If scope offirstCtrl
is parent of thesecondCtrl
scope: