This question already has answers here:
What does this symbol mean in JavaScript?
(1 answer)
What do square brackets around a property name in an object literal mean?
(2 answers)
Closed 3 months ago.
What is happening in the code on line 10 ({[last]: newObj}) in the following snippet:
How is JS able to use the value of parameter last instead of using last as the property name?
let first = 'first';
let last = 'last';
function foo(first, last) {
let newObj = {
name: 'newObj'
};
let obj = {};
Object.assign(obj, {[last]: newObj});
return obj;
}
console.log(foo('bye', 'hey')); // { hey: { name: 'newObj' } }
Thanks.
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!
This question already has answers here:
Why is a global `name` variable declared in typescript and can I avoid using it?
(1 answer)
Is variable called "name" always defined in Javascript?
(2 answers)
Closed 4 years ago.
I have this code:
const employee = {
name: 'Jen Baker',
getName: function() {
return this.name;
}
};
const getEmployeeName = employee.getName;
console.log(getEmployeeName()); // returns '' (empty string)
Why does getEmployeeName() return an empty string? Shouldn't it be undefined because it has lost reference to this.name in the employee object? Where did the empty string come from?
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);
This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 7 years ago.
var obj = {a:1,b:2};
var str = "a";
console.log(obj.str);
This outputs undefined. What am i missing here ?
You need to use []
var obj = {a:1,b:2};
var str = "a";
console.log(obj[str]);