What does this syntax mean in js? [duplicate] - javascript

This question already has answers here:
Two sets of parentheses after function call
(4 answers)
Closed 4 years ago.
const List = connect(mapStateToProps)(connectedList);
There is a function called "connect" being called with "mapStateToProps" as the argument, however there is "connectedList" surrounded by parenthesis right after the function calling. I haven't seen that before and I didn't find anything about that in the es6 articles I read for research.

The connect function most likely returns another function that accepts one argument which is being invoked.
function getFunc(arg){
alert(arg);
return function(arg2){
alert(arg2);
}
}
getFunc("arg1")("arg2");//invokes the getFunc function and invokes the returned function

Related

Javascript Behavior when Function.prototype.bind is called serially [duplicate]

This question already has answers here:
Can you rebind a rebound function using `bind`
(3 answers)
How to override 'this' parameter of a bound function in javascript
(3 answers)
Javascript function bind override (how to bind it to another object)
(2 answers)
Closed 4 months ago.
I have a question about Function.prototype.bind.
function f() {
return this;
}
const g=f.bind({foo:'foo'});
const h=g.bind({bar:'bar'});
console.log(h()); // expected {bar:'bar'} but get {foo:'foo'}
I was expecting the above snippet to produce either {foo,bar} or {bar} but instead I get {foo}.
Can you explain what is going on?
Using .bind() to create a function that "fixes" the value of this in the called function results in a function where this cannot be overridden.
What you get back from .bind() is a new function that will pass along arguments to the original target function, with a guarantee that in that called function the value of this will be what you ask for. Re-binding that function therefore does no good: you do get back a new function, but it calls a function (the first bound function) that explicitly ignores its own this value.

JS function inside a parenthesis with a parenthesis at the end with an argument, what does it mean? [duplicate]

This question already has answers here:
What is the purpose of a self executing function in javascript?
(21 answers)
Closed 6 years ago.
What does this code do? Can someone describe why the function is inside a parenthesis and also why it has a parenthesis at the end and what it is doing?
(function (innerKey) {
//doSomething
}(key));
You are creating the function and invoking it at the same time with the key value filling the innerkey parameter.
It's a self-invoking anonymous function. It will be called immediately after loading the script, and it will take the element inside the brackets key as the function's argument.
You can read more here:
What is the (function() { } )() construct in JavaScript?

What is the difference between writing a function with or without parentheses inside a function in jQuery? [duplicate]

This question already has answers here:
When to use () after a callback function name? [duplicate]
(5 answers)
Closed 6 years ago.
Well I'm starting with jQuery and I'm wondering what is the difference between writing a function with or without parentheses inside a function.
For example if I have the following function:
function test(){
alert("pen pineapple apple pen");
}
This:
$(document).ready(test);
and this:
$(document).ready(test());
both show the same result: "pen pineapple apple pen"
Putting parentheses () at the end of a function causes that function to be invoked immediately, and use its return value in the expression.
This code $(document).ready(test); is using test as a callback function. It essentially says: when the document becomes ready, call the function that I'm providing you with (test).
This code $(document).ready(test()); is immediately invoking the function test, having it return a value, and then passing that value to the ready method. It's possible that test is returning a different function here, which in turn will act as the required callback function. It could also just be an error though, with someone inadvertently including the parentheses when they shouldn't have.

Javascript: Why name object method functions [duplicate]

This question already has answers here:
Why use named function expressions?
(5 answers)
Closed 6 years ago.
In some projects I can see, that the functions wich are object methods get names after the function constructor - I can not see why, can any one explain?
Example: named
someObj.prototype = {
load: function someObj_load(file) {
vs unnamed
someObj.prototype = {
load: function(file) {
I can not see any advantage in the above.
So you can see the name of the function name instead of Anonymous function in stack traces. I think some browsers will pick up the name of the variable/attribute you've assigned it to. Some don't.

Is there any way, in javascript, to obtain the name of the function, inside the function which was called [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Can I get the name of the currently running function in javascript?
Get function name in javascript
Is there any way, in javascript, to obtain the name of the function, inside the function which was called.
function something() {
console.log( "The name of the function you invoke is " ...should says 'something' );
}
yes.
function something(){
alert(arguments.callee.name);
}

Categories

Resources