This question already has answers here:
How to bind methods when destructuring an object in JavaScript?
(5 answers)
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed 4 years ago.
This code in ES6:
var myObj = {firstname: "Stevey",
printFirstName() {
console.log(this.firstname);
}}
function printNames({printFirstName}) {
printFirstName();
}
printNames(myObj);
TypeError: Cannot read property 'firstname' of undefined. How to fix this?
Edit: This question is not a duplicate. The code in 'exact' answer marked, does not take function from deconstructed object as function param. And the other answer marked is an essay on how 'this' works. That is just a reference not the direct answer.
Related
This question already has answers here:
Methods in ES6 objects: using arrow functions
(6 answers)
Arrow Function in Object Literal [duplicate]
(1 answer)
Closed 24 days ago.
image showing the function i created
the hello function of the returned object logs out undefined, instead of reference to the reply object or the returned object and I don't know why
I haven't really tried anything.
This question already has answers here:
JavaScript object functions and `this` when unbound and returned in expression/parens
(2 answers)
Closed 3 years ago.
I came across this (document.createElement)('a');.
How is it different from document.createElement('a');
No difference.
Please read doc for more.
This question already has answers here:
What does this symbol mean in JavaScript?
(1 answer)
What does curly brackets in the `var { ... } = ...` statements do?
(4 answers)
What do {curly braces} around javascript variable name mean [duplicate]
(1 answer)
Closed 4 years ago.
I was reading some JavaScript code and saw a line of code similar to this.
const {name, password, email} = user;
I tried searching around but could not figure out what this syntax is called so I am not able find the documentation. Is this a new JavaScript syntax? What is it called? What does it do exactly?
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 4 years ago.
I've been grappling with this issue for about an hour now, here is the offending code:
const t = games[0];
for (const mvar in t) {
if (t.hasOwnProperty(mvar))
console.log(`${mvar}: ${t.mvar}`);
}
The output is:
appid: undefined
name: undefined
playtime_forever: undefined
img_icon_url: undefined
img_logo_url: undefined
has_community_visible_stats: undefined
However, the WebStorm debugger says this value is not undefined as such:
Are there any other reasons that this could be?
t has no property named myVar.
You want t[myVar] to get the property with that name.
This question already has answers here:
JavaScript function aliasing doesn't seem to work
(6 answers)
Closed 8 years ago.
How come the code below doesn't work?
var x = {};
x.a = alert;
x.a('asdf'); // TypeError: Illegal invocation
Because the internals of the alert function requires that the value of this be window.
x.a.call(window,'asdf');
… will work.