Because implementations of the language are likely to use primitive types as value types rather than reference types. That is, when you assign a value to a new variable, rather than changing the variable such that a contains a reference to the same object used in the source expression, it might copy the value of that object into the variable (if you know C#, think about the different between a struct
and a class
). Such a value therefore could not sensibly be used as part of a weak referenced storage system, as there is no object to be garbage collected (just values that get copied around) and therefore the weak key removal would never trigger.
The use of values rather than references for these common data types is an optimisation that can produce substantial performance improvements, and therefore the developers of ECMAscript would have been careful to avoid adding any features that mean it couldn't be used.
If you need to use such an item, you can simply create a container object (usually called a "box") to keep the value and allow it to be used in contexts where only references are permitted.
const foo = (property) => { return property + 'bar' }
In JavaScript, functions are objects. That means they can be passed around and assigned to variables (I mean, other languages support it too and are adapting it more and more, but JavaScript is pretty much build on that).
In order to understand ES6 fat arrow functions, it's good to know what the function expands to as well as the few new rules that come with ES6 anonymous functions, such as:
- lexical
this
- implicit return
- not having to use brackets for a single parameter functions
With that knowledge in mind the mentioned piece of code may look like any of the following variants:
const foo = property => { return property + 'bar' }
const foo = (property) => property + 'bar'
const foo = property => property + 'bar'
And it all expands to:
var foo = function(property) {
return property + 'bar'
}
If you are familiar with pointers in languages like C or C++, what the fat arrow syntax does is it creates a function and returns a function pointer to it which may be assigned to a variable on the left side of the equation.
That is the main thing you got wrong, you are not returning some magical property which is assigned to the constant foo
variable, the property is a function, and that's it.
So while the initial Redux code might look a little off-putting at first it really expands to nothing else than a few chained functions within each other.
var actionLogger = function actionLogger(input) {
var dispatch = input.dispatch;
var getState = input.getState;
return function (next) {
return function (action) {
console.log(action);
return next(action);
};
};
};
The actions logger is then called like this:
// You pass a dispatch and getState variables to it
var dispatchedLogger = actionLogger({dispatch: someDispatch, getState: someState});
// You define a function that is supposed to fire after each action
// Each action type will be modified by apending 'bar' to it
var withFollowUpAction = dispatchedLogger(action => action + 'bar');
// Finally you run it with the desired action
withFollowUpAction('someAction');
Now I haven't really worked with Redux, but I believe the reason why the functions are chained the way they are is because of the API of the Redux library, which knows how to consume it.
Best Answer
See the proposal to add arrow functions: http://wiki.ecmascript.org/doku.php?id=harmony:arrow_function_syntax1
What it says is:
You can also see some discussion of a previous version of the proposal which did have the -> syntax as well: https://esdiscuss.org/topic/arrow-function-syntax-simplified
It appears to come down to the following:
->
was deemed rarely useful, and a foot-gun.