How are these methods of writing functions different? [duplicate] - javascript

This question already has answers here:
How does this object method definition work without the "function" keyword?
(2 answers)
ES6 Object Literal Syntax for Methods
(1 answer)
Closed 2 years ago.
I have code that has the following functions written:
testing: function() {
///
}
what is the difference of writing it as :
testing() {
///
}
Do they mean the same thing and have the same purpose?

Related

Unexpected hehaviour when defining vairables [duplicate]

This question already has answers here:
JavaScript 'hoisting' [duplicate]
(5 answers)
Javascript function scoping and hoisting
(18 answers)
What are the precise semantics of block-level functions in ES6?
(2 answers)
Closed 1 year ago.
I have come across a very strange JavaScript code. Its execution result is completely different from what I expected. I want to know why.
var  a  =  1999;
{
console.log(a);
a  =  2020;
function  a()  {}
a  =  2021;
}
console.log(a); //Why 2020?

What is this syntax for creating an object? [duplicate]

This question already has answers here:
How does this object method definition work without the "function" keyword?
(2 answers)
No colon after property name in object declaration, is it valid? [duplicate]
(2 answers)
Closed 2 years ago.
I'm reading about mixins here and it uses this valid code:
let sayHiMixin = {
sayHi() {
alert(`Hello ${this.name}`);
},
sayBye() {
alert(`Bye ${this.name}`);
}
};
console.log(sayHiMixin);
sayHiMixin is a valid object. Can anyone point out to me how the syntax for writing this object is valid (maybe an MDN page)? I've never seen it before except for when writing methods in a class.

Immediately executed anonymous function syntax ambiguity [duplicate]

This question already has answers here:
Immediate function using JavaScript ES6 arrow functions
(3 answers)
Why are parentheses required around JavaScript IIFE? [duplicate]
(3 answers)
Closed 3 years ago.
Why is the following a syntax error?
(()=>{console.log(1)}())
()=>{console.log(1)} is clearly(?) an expression that evaluates to a fat arrow function, and () "should" call it?
Presumably because it is ambiguous. What is the ambiguity?
Obviously, I realise the following works:
(()=>{console.log(1)})()

Calling function using a pair with first element '0' and second element the function itself [duplicate]

This question already has answers here:
Calling function with window scope explanation (0, function(){})()
(2 answers)
JavaScript syntax (0, fn)(args)
(2 answers)
(1, eval)('this') vs eval('this') in JavaScript?
(4 answers)
What is the meaning of (0, someFunction)() in javascript [duplicate]
(3 answers)
What is the purpose of calling (0, func)() in JS? [duplicate]
(1 answer)
Closed 2 years ago.
I've seen that code in open source projects and I wonder what is its intent:
'use strict';
function invariant(condition, message) {
if (!condition) {
throw new Error(message);
}
}
(0, invariant)(false, 'failed');
Is this done to disable assertions? If it is, then I was unable to figure out how.

What (jQuery) means in the end of function? [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
What is the purpose of wrapping whole Javascript files in anonymous functions like “(function(){ … })()”?
(10 answers)
Closed 7 years ago.
I have a function:
var waitingDialog = (function($){
.....
return{
}
})(jQuery);
Also could you explain what $ means in the function? Is it going to work without that?
It means that jQuery (if it exists) will be passed to the function. $ is merely the name that variable will take in the scope of the function.
There is a section about that in official jQuery website : https://learn.jquery.com/plugins/basic-plugin-creation/#protecting-the-alias-and-adding-scope

Categories

Resources