why my arrow function inside a object doesn't works? [duplicate] - javascript

This question already has answers here:
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
Methods in ES6 objects: using arrow functions
(6 answers)
Closed 2 years ago.
please, I have a doub
Why with arrow function inside in a object doesn't work?
How we do to can make it works with arrow function?
let person = {
name: "Homer Simpson",
birdYear: 2000,
getAge: function() { // WORKS!
return 2020 - this.birdYear
},
getAgeWithArrowF: () => { // Doesn't works
return 2020 - this.birdYear // using person.birdYear wrks but i need with (this.birdYear)
// cant reach birdYear =(
}
};
// The call
console.log(person.getAge()); // 2020
console.log(person.getAgeWithArrowF()); // NaN
THANKS!

Related

What is the difference between creating object with NEW keyword using constructor and plain object regarding arrow functions methods? [duplicate]

This question already has answers here:
ES6 Arrow function: why "this" points differently when used in constructor and object literal? [duplicate]
(3 answers)
Methods in ES6 objects: using arrow functions
(6 answers)
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed 1 year ago.
I have a Function constructor A
function A(){
this.number = 10;
this.show = () => console.log(this.number)
}
So, I'm creating an object with this constructor
let a = new A();
//a={
// number: 10,
// show: () => console.log(this.number)
//}
And plain object b
let b = {
number: 20,
show: () => console.log(this.number)
}
As result, I can say objects a and b syntactically equal a == b, but why
a.show() // gives 10
b.show() // undefined
I understand that for b, 'this' is a global object, that is why it is undefined in this case, but why there is different behavior for a?

Wsy this arrow function doesn't work with 'arguments.length'? [duplicate]

This question already has answers here:
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
Why do arrow functions not have the arguments array? [duplicate]
(1 answer)
Closed 2 years ago.
I'm wondering why the following arrow function, named 'countArg2' doesn't work.
Is there anybody can explain what's wrong please?
This works
function countArg1() {
return arguments.length;
}
countArg1(1, 2, 3); //3
This doesn't work..
const countArg2 = () => arguments.length;
countArg2(1, 2, 3);
// VM6745:1 Uncaught ReferenceError: arguments is not defined
Thank you in advance.
You have to parse the arguments to the arrow function like this
const countArg2 = (...arguments) => arguments.length;
console.log(countArg2(1, 2, 3));
// VM6745:1 Uncaught ReferenceError: arguments is not defined
// at mArgs (<anonymous>:1:29)
// at <anonymous>:2:1

what is (function(){}()) in object when i make a function [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
What is the purpose of a semicolon before an IIFE? [duplicate]
(1 answer)
What does the leading semicolon in JavaScript libraries do?
(6 answers)
What are the rules for JavaScript's automatic semicolon insertion (ASI)?
(7 answers)
Closed 3 years ago.
if I remove the ";" after let self = this; then it does not work
let obj = {
name: 'john',
getInfo: function() {
let self = this;
(function() {
console.log(self.name)
}())
}
}
obj.getInfo();
You don't have to use this and ;, and the code below makes it much simpler without using this, nor ;:
let obj = {
name: 'john',
getInfo: () => console.log(obj.name)
}
obj.getInfo();

What is the difference between using const and function in javascript? [duplicate]

This question already has answers here:
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
Closed 4 years ago.
What is the difference between these two functions?
const square = (number) => {
return number * number;
};
function square (number) {
return number * number;
}
There are several.
First, const prevents reassignment of the name square while function does not. Second, using an arrow function doesn't have it's own lexical context, so it won't have a scoped this and can't be used as a constructor. For reference, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
Note you can also do:
const square = function(num) { return num * num }
Which both prevents reassignment and creates a lexical context.

Javascript "=>" syntax meaning [duplicate]

This question already has answers here:
What's the meaning of "=>" (an arrow formed from equals & greater than) in JavaScript?
(14 answers)
Closed 7 years ago.
Code example:
function unusedDigits(...args){
return [0,1,2,3,4,5,6,7,8,9].filter(o => args.join("").indexOf(o) === -1).join("")
}
Everything is clear here. Exept =>. What does this mean in javascript?
This is a ES6 arrow function which is a short syntax for function expression. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
So:
// ES5
var selected = [0,1,2,3,4,5,6,7,8,9].filter(function (o) {
return args.join("").indexOf(o) === -1;
});
// ES6
var selected = [0,1,2,3,4,5,6,7,8,9].filter(o => args.join("").indexOf(o) === -1);

Categories

Resources