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

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.

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!

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

How to multiply objects by specified rules? [duplicate]

This question already has answers here:
Javascript: operator overloading
(9 answers)
Closed 4 years ago.
Let's say i want an object that when multiplied with, it multiplies by two and subtracts 1.
The syntax would look like this:
var a = {
on_multiply: function(context){
return context*2-1
}
};
alert(2*a);
This would output 3.
I don't want to write
"a.on_multiply(2)"
Is there a way to do this?
If yes, is it possible to do this with arrays or matrixes also?
The simplest way I can think of to make the above example work is to assign a function named a, and have the context as a parameter of that function:
function a(context) {
return (context * 2) - 1;
}
And if you really wanted a to be a function assigned to a name:
const a = context => 2 * context - 1;
And the above in ES5 syntax:
const a = functipn(context) {
return (context * 2) - 1;
}
Hopefully this helps!

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