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 want to add a property to the object Array. I've been trying the following:
Array.prototype.isEmpty = array => (array || []).length <= 0 // only works if I use array.isEmpty(array) which isn't exactly what I want.
Array.prototype.isEmpty = (() => (this || []).length <= 0)() // always returns true since this is always undefined.
Is there a way to add this in JavaScript?
Related
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:
ES6 concise methods and non-concise methods in object literals
(1 answer)
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
Methods in ES6 objects: using arrow functions
(6 answers)
Closed 2 years ago.
I'm still not very familiar with all the ES6 syntax, and I'm starting to learn Vue.js, which makes extensive use of it. So, I find something that puzzles me a bit:
What's the difference between these two methods?
const actions = {
foo1(num) {
console.log(num);
},
foo2: (num) => {
console.log(num);
}
}
actions.foo1(2);
actions.foo2(3);
// output: 2
// output: 3
I guess they are not the same, although they both output their passed value.
This question already has answers here:
Does TypeScript have a Null-conditional operator
(8 answers)
JavaScript, elegant way to check nested object properties for null/undefined [duplicate]
(4 answers)
Closed 3 years ago.
EDIT: Existing answers are 2 and 5 years old (from 2017 and 2014).
How can I shorten the following statement
ratingsExisting(): boolean {
return (
this.fromAPIData.book &&
this.fromAPIData.book.misc &&
this.fromAPIData.book.misc.ratings &&
this.fromAPIData.book.misc.ratings.length > 0
);
}
so that I don't receive errors if any of the properties is null?
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.
This question already has answers here:
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
What's the meaning of "=>" (an arrow formed from equals & greater than) in JavaScript?
(14 answers)
What does this symbol mean in JavaScript?
(1 answer)
Closed 5 years ago.
I don't understand how the expression of return of following code works.
function reverse3(array) {
return array.map((element, idx, arr) => arr[(arr.length - 1) - idx]);
}
This function gives reverse of array parsed to the function. But I actually don't understand and didn't get idea after googling for hours about how that statement in map() function works. Please give me explanation about for what those three variables stands and what that => operator does. THANKS in advance!!