This question already has answers here:
What does "this" refer to in arrow functions in ES6?
(10 answers)
Difference between this ES6 arrow function and regular function? [duplicate]
(2 answers)
Closed 6 years ago.
I've got a code in js/jQuery for an event which is.
function extraAction(data) { console.log(data); }
$('a.edit').on('click', function(e) {
e.preventDefault();
var d = $(this).attr('href');
extraAction(d);
});
The code works just fine. But if I try to declare the function(e) as (e) => instead, I get an undefined in the console log. Why is that?
It works regardless if I pass a static string like extraAction('hello');
Related
This question already has answers here:
Why do I have to omit parentheses when passing a function as an argument?
(2 answers)
JS: What's the difference between a ! closure and () closure? [duplicate]
(2 answers)
Closed 6 months ago.
I have a question. Why when I'm using anonymous function and I want to call it immediately i have to do like that:
(() => {console.log('something')})()
But when I want to use it with setTimeout for example, I don't have to use '(' at beginning and '()' at the end?
setTimeout(() => {console.log('something')}, 2000)
You pass the function as a parameter to setTimeout, rather than calling it right away. That's why you don't need the () at the end
This question already has answers here:
Arrow Functions and This [duplicate]
(5 answers)
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
Closed 1 year ago.
Why is $(this) returning undefined instead of the clicked element?
I've replaced $(this) with event.target and it works fine but would like to understand the issue with $(this), thank you!
$('.nav-search-options').on('click', (event) => {
console.log($(this))
})
#JamesJavascript.
It is partially right what Justinas said in the comment about arrow function - although there are more things to add.
Arrow functions do not have their own "this", instead they bind one from their parent scope.
To see what I mean, try this and see that it returns you the window object:
const myFunction = () => {
console.log(this);
};
// call it
myFunction();
Read more about it here - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 2 years ago.
I came across with a piece of code with new syntax for me, What this syntax means in js? I mean this parentheses at the beginning:
(function () {
//...
})()
It's called IIFE
basically, you can define a a function and invoke it immediately without declaring a name for it.
This question already has answers here:
Use of 'prototype' vs. 'this' in JavaScript?
(15 answers)
Prototyped and a non-prototyped method? [duplicate]
(1 answer)
Closed 8 years ago.
Consider the following code:
function someObj() {
this.someFunction = function() {
console.log('someFunction');
}
}
someObj.prototype.foo = function() {
console.log('foo');
}
I can call both like so:
var test = new someObj();
test.someFunction(); // Logs 'someFunction'
test.foo(); // Logs 'foo'
How would variable scope be affected here? Is there an advantage / disadvantage to each approach?
This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 9 years ago.
I'm trying to understand the difference between :
var x = function () { ....}
(function () { ....} ) ();
I get it that the first function will put the results on x.
that and when exactly the second one will be fired? and why do i need the (); at the end?
This is an example of the Immediately-invoked function expression.
The function is executed immediately because () is how JavaScript calls functions. The syntax might be confuse you, because the function does not have a name, but ( function(){} )() just immediately calls the function with no arguments.