This question already has answers here:
What's the meaning of "=>" (an arrow formed from equals & greater than) in JavaScript?
(14 answers)
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
Closed 4 years ago.
I have es6 class with constructor and validation method.
class Popups {
constructor(selector) {
this.app = selector;
}
validate() {
$.validator.addMethod('atLeastOneLowercaseLetter', (value, element) => {
return this.optional(element) || /[a-z]+/.test(value);
// how to replace `this` ??
},
'Must have at least one lowercase letter'
);
}
}
My this refers to my Class, but I need get $.validator with my validate form. How i can replace this?
If i write $.validator.optional(element) || /[a-z]+/.test(value) I get error $.validator.optional is not a function
In that case don't use an arrow function as it does not have it's own this. Use a normal function expression as the callback instead.
Related
This question already has answers here:
What does "this" refer to in arrow functions in ES6?
(10 answers)
Methods in ES6 objects: using arrow functions
(6 answers)
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
Closed 8 months ago.
i just created an object with an arrow function :
const user={
fName:"0xN1nja",
about:()=>{
return this.fName;
}
}
now, when i call that arrow function with .call() method, it returns undefined :
a=user.about.call(user);
console.log(a);
>>> undefined
But when i replaced that arrow function with a normal function expression, it returns the value
const user={
fName:"0xN1nja",
about:function(){
return this.fName;
}
}
a=user.about.call(user2);
console.log(a);
>>> 0xN1nja
what is happening here?
PS : im new to javascript
Arrow functions don't bind to this as function do. That's one of the main reason they were introduced (probably the most important one).
You cannot even bind them.
This question already has answers here:
When should I use a return statement in ES6 arrow functions
(6 answers)
How is () => {...} different from () => [duplicate]
(4 answers)
Arrow function without curly braces
(9 answers)
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
Closed 2 years ago.
This probably suits other methods as well but the one i'm using at the moment is map().
How come:
const singleFruit = fruits.map((fruit) => fruit.toUpperCase());
console.log(singleFruit);
returns the array the correct way, with everything in uppercase, when:
const singleFruit = fruits.map((fruit) => {
fruit.toUpperCase();
});
console.log(singleFruit);
gives me an array but with my assigned fruits are now undefined. I can solve this by adding return before "fruit.toUpperCase();" I thought that the second code was the exact same as the first one but without the curly braces.
Thanks!
This question already has answers here:
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
Closed 2 years ago.
$('#title').keyup( () => {
const title_val = $(this).val();
console.log('before');
if (title_val.length >= 20){
console.log(title_val);
}
console.log('after');
});
console.log('before') and console.log('after') works. If-statement only works if I use function() instead of () => {...}, even if the condition is satisfied. Why does it behave like this?
There is no this operator in arrow functions, only in function functions..
when using this in an arrow function, it doesn't refer to the function scope; rather, to the parent scope (usually window)
This question already has answers here:
Methods in ES6 objects: using arrow functions
(6 answers)
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
Closed 4 years ago.
i have this object literal
let p = {
name : 'Amir',
say: () => {
console.log(this.name)
console.log(this)
}
}
and I want the say function works using this
p.say();
but the arrow function obviously gets the window object as 'this'. I know I could use a regular function for 'say' instead of arrow and it will work fine.
BUT
I would like to ONLY change the call to say function to make it work, but the binding won't work.
I mean something like p.say.bind(p)() or p.say.call(p) aint gonna work as desired.
Is it possible to change the call to function ONLY and not the say function?
This question already has answers here:
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
Using arrow functions with .call()? "This" varies based on if in devtools or iojs locally? Should one avoid using arrows with .call?
(1 answer)
Can you bind 'this' in an arrow function?
(14 answers)
Closed 5 years ago.
I know about using .call() and .apply() to set the this for a function call, but can I use it for a lambda?
Consider this example from the MDN webdoc for Function.prototype.call:
function greet1() {
var reply = [this.person, 'Is An Awesome', this.role].join(' ');
console.log(reply);
}
let greet2 = () => {
var reply = [this.person, 'Is An Awesome', this.role].join(' ');
console.log(reply);
}
var i = {
person: 'Douglas Crockford', role: 'Javascript Developer'
};
greet1.call(i); // Douglas Crockford Is An Awesome Javascript Developer
greet2.call(i); // Is An Awesome Javascript Developer
Why doesn't the greet2 call work the same was as greet1 and is there a way to set this in the case of greet2?