JavaScript (function(){...}( )) and (function(){...} ) ( ) [duplicate] - javascript

This question already has answers here:
Location of parenthesis for auto-executing anonymous JavaScript functions?
(4 answers)
Closed 7 months ago.
I've inherited a project with some Java Script. I know nothing about js. There are some auto-build steps involved, the result of which is an anonymous function. The version is version control is different from the version I build, even though the environments were supposed to be the same.
One version is, void parameter list inside the evaluation brackets:
(function(){...}( ))
The other version is, void parameter list outside the evaluation brackets:
(function(){...} ) ( )
Are these two forms technically the same? Is one form technically an error? Is either form actually an error? Or what?

In this case, both are equivalent and valid.
Note that for the first option, when the outer parentheses are excluded, that will result in a SyntaxError
function(){...}() // error
(function(){...}()) // no error
(function(){...})() // no error

Related

What does the array.pop()!(object) syntax in Codemirror do? [duplicate]

This question already has answers here:
In Typescript, what is the ! (exclamation mark / bang) operator when dereferencing a member?
(5 answers)
Two sets of parentheses after function call
(4 answers)
Closed last month.
In Codemirror 6's documentation, and in the code line 41, ...
while (pending.length) pending.pop()!(data.updates)
What does this syntax mean?
Seems to be typescript specific.
What would be the javascript equivalent?
In TypeScript, the ! operator can also be used to assert that a value is non-nullable. This is called a non-null assertion operator, and it is used to tell the TypeScript compiler that a value is definitely not null or undefined. Check the documentation
The pop() method removes the last element from an array and returns that element. See the documentation.
Since the return value is a function, well you can call it.

When I try to use ``` ...``` decorator doesn't work [duplicate]

This question already has answers here:
ECMAScript 6 arrow function that returns an object
(6 answers)
Closed 3 years ago.
I am trying to do this :
setMyState(prevState=> {...prevState, name: e.nativeEvent.text });
While the console says src/components/addItem.js: Unexpected token
And it doesn't work :(
While using a js file .. I tried with .jsx too and same error :(.
Also I found an answer here WebStorm error: expression statement is not assignment or call
but it didn't solve my problem since when I run the app now it crashes exactly there ...
If you use an arrow function and want to return an object you need to wrap your object with (). If you don't use, arrow function thinks {} is the body block. So, try using:
setMyState(prevState=> ({...prevState, name: e.nativeEvent.text }));

What does "+" means in +function($)? [duplicate]

This question already has answers here:
JavaScript plus sign in front of function expression
(4 answers)
Closed 5 years ago.
I had look at this question to know about what this means.
(function($) {
})(jQuery);
I am looking at different bootstrap plugins, which have +function ($), while defining the function.
What does + does here, does it appends this function to other functions ?
To guide the javascript parser, that the thing written near the unary operator + is an expression.
EDIT: You can create javascript functions anonymously. This is one of the syntaxes to create the same. By doing so, when they are called (i.e evaluated), they act like a returning a function value. You can read more from the second link which provides a good description of it.
This link explains it well
Once declared, if not named, these can be executed inline like IIFE (Immediately invoked function expressions) as well. And in this form, they can then be used to create plugins or used as namespaces and attached to window object or jquery object for use later.
Good sample file to see anonymous function code in action
http://www.programering.com/a/MTMwITMwATk.html
It's a bang function
the + operator is faster than usual !
see more at
javascript function leading bang ! syntax

Is this jQuery related, and what does this mean? [duplicate]

This question already has answers here:
What does the exclamation mark do before the function?
(8 answers)
Closed 9 years ago.
I have been searching, but no clues... or I haven't search the proper way (so please excuse me if duplicate)
Does the following code, mean: If there is no jQuery defined, or no document ready?
!function ($) {
///
!function ($) {
$(function(){ // I know this is an alias to $(document).ready()
.....
}(window.jQuery) // Ending of !function
I'm asking, because I saw it here: http://twitter.github.io/bootstrap/assets/js/application.js and have no I really don't know what it means.
In this case, ! is being used because it's an operator, so the rest of the line will be treated as an expression rather than a statement. This is a way of writing an immediately invoked function expression. The more common idioms can be found here:
Javascript immediately invoked function patterns
! on a function(){}() simply flips (or negates) the value that's returned after immediately calling the function that's defined. Notice that immediately after the function definition, at the very last line, it says (window.jQuery) — that's passing jQuery as the argument to the function and calling it immediately.
But in this case it doesn't appear to do anything important since the return value won't be used anyway. The function will still be executed though.
Also, it says this at the top of the file:
// NOTICE!! DO NOT USE ANY OF THIS JAVASCRIPT
// IT'S ALL JUST JUNK FOR OUR DOCS!
// ++++++++++++++++++++++++++++++++++++++++++
So that's evidence further that it's not meant to serve any real purpose.

Usage of toString in JavaScript [duplicate]

This question already has answers here:
Calling member function of number literal
(3 answers)
Closed 9 years ago.
I'm reading through Douglas Crockford's JavaScript: The Good Parts, and I'm at the point where he defines a fade function. Part of this code boils down to this:
var level = 1;
var hex = level.toString(16);
So I run this in my browser's console to see what I get....
var level = 1;
level.toString(16);
Hey, it returns "1"... Fabuloso! Wunderbar!
Then to be cheeky, I try this to see what I get...
1.toString(16);
And I get
SyntaxError: Unexpected token ILLEGAL
What the what? If level is a variable equal to 1, and running this method on level works fine, then why doesn't running this method on the actual number 1 work? I tried a similar experiment with the toPrecision() method and that worked fine in both cases. What's the issue here? Is this another one of those inherent flaws in the JavaScript implementation, or am I missing something? I am testing in Google Chrome.
Related: Stack Overflow question Why don't number literals have access to Number methods?.
It's just a language grammar limitation.
Since 1. is a legal literal number (and 1.t is not) the tokeniser will split this into the following tokens:
1.
toString
(
)
And that's an illegal sequence of tokens. It's object method, instead of object . method.
In the working versions in #Joey's answer, the braces prevent the tokenizer from treating the dot as part of the number literal instead of as a separate token, as does writing:
1.0.toString()
or
1..toString()
since the tokenizer knows that the second dot must be a token on its own, and not part of the number literal.
You need 1..toString or (1).toString to get the number literal
level is a variable (and thus an object).
1 is a literal. They are not objects and the interpreter thinks about them completely differently.
http://www.cs.brown.edu/courses/bridge/1998/res/javascript/javascript-tutorial.html#4

Categories

Resources