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

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();

Related

Odd syntax for setting an object's property in JavaScript [duplicate]

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.

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!

Why is this not undefined? [duplicate]

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?

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);

Cannot access property via dot notation javascript [duplicate]

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]);

Categories

Resources