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

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

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?

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

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!

ReferenceError: Cannot access 'obj' before initialization [duplicate]

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Closed 3 years ago.
I have this object:
const obj = {
thing: 5,
layer: {
otherThing: obj.thing - 2
}
}
error:
ReferenceError: Cannot access 'obj' before initialization
I tried to use this but it didn't work as expected.
This is not something you can do in JavaScript. But you have two possible alternatives here:
1)
const obj = {thing: 5, layer: {}};
obj.layer.otherThing = obj.thing - 2;
2) getters
const obj = {
thing: 5,
layer: {
get otherThing(){obj.thing - 2}
}
}

Using this in an anonymous function in Javascript / Chrome Browswer gives error: Cannot read property 'localeCompare' of undefined [duplicate]

This question already has answers here:
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
Closed 3 years ago.
I ran into this error of "Cannot read property 'localeCompare' of undefined"
when I was trying to convert a string function of compare(String, String) to String.compare(String)
let compare = (y, x) => y.localeCompare(x) == 0 ? true : false; //This works
let gender = x => compare("male", x) || compare("female", x); //This works
String.prototype.compareTruthy = (x) => {
this.localeCompare(x) == 0 ? true : false;
}
"male".compareTruthy("male") //This does not work, why?
I wanted to re-use this compareTruthy function for any other string comparisons I might have later on.
What am I missing in my understanding?
Arrow function expressions don't have the context of the current object. Read this page.
You need to use:
String.prototype.compareTruthy = function(x) {
.....
}

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.

Categories

Resources