In Meteor Whatsapp example project is file where "=>" is used, but my WebStorm IDE detect it as error. I can't find any docs about this syntax.
chats.forEach( chat => {
let message = Messages.findOne({ chatId: { $exists: false } });
chat.lastMessage = message;
let chatId = Chats.insert(chat);
Messages.update(message._id, { $set: { chatId: chatId } })
});
GitHub repository for bootstrap.js file is here
What is "=>" ?
It's an arrow function, newly defined in ES6. An arrow function expression has a shorter syntax than a function expression and does not bind its own this, arguments, super, or new.
Use === if you want to compare couple of things in JavaScript, it's called strict equality, it means this will return true if only both type and value are the same, so there wouldn't be any unwanted type correction for you, if you using == , you basically don't care about the type and in many cases you could face ...
The main difference between the == and === operator in javascript is that the == operator does the type conversion of the operands before comparison, whereas the === operator compares the values as well as the data types of the operands.
Double Equals ( == ) checks for value equality only. It inherently does type coercion. This means that before checking the values, it converts the types of the variables to match each other.
I was actually about to downvote this question, but googling the answer proved surprisingly difficult if you don't already know what its called. As you can see in the links in the comments, that's a fat arrow function (sometimes referred to as just an arrow function).
There are some confusing aspects of arrow functions, so I'll hit some highlights:
Normal functions have a this
pointer set depending on the context: functions called with new
have it set to the newly-created object, functions called as methods have it bound to the object the method was called from, its otherwise bound to undefined
or the global object (depending on the 'strict mode' pragma), and can of course be set with Function.prototype.bind
et al.
But arrow functions have no binding for the this
pointer created by the runtime (nor can it be specified via Function.prototype.bind
), meaning it gets lexically looked up through scope chain resolution just like any other var. The MDN article is at best slightly confusing on this point (see link above).
Additionally, arrow functions have an implicit return, the return value will automatically be the last evaluated expression in the function body.
Arrow functions have no arguments
psuedo-array. You can use ES 6 rest parameters instead.
For functions of arity 1, the parens around the parameter may be omitted.
That's an es6 arrow function. If you switch to WebStorms settings:
you can switch your javascript version to ecmascript 6 like shown in the picture to make WebStorm recoginze them properly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With