This question already has answers here:
How does this object method definition work without the "function" keyword?
(2 answers)
No colon after property name in object declaration, is it valid? [duplicate]
(2 answers)
Closed 2 years ago.
I'm reading about mixins here and it uses this valid code:
let sayHiMixin = {
sayHi() {
alert(`Hello ${this.name}`);
},
sayBye() {
alert(`Bye ${this.name}`);
}
};
console.log(sayHiMixin);
sayHiMixin is a valid object. Can anyone point out to me how the syntax for writing this object is valid (maybe an MDN page)? I've never seen it before except for when writing methods in a class.
Related
This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
What is the difference between "let" and "var"?
(39 answers)
Closed 12 days ago.
I am getting confused regarding the this keyword in JS. I am not getting how this will work in a standalone function. I referred to many sites, all they are saying is when this is given in standalone function it will refer to the global object.
In that case for the following code I should get name as "steve" but I am getting undefined. I am trying to understand it but I couldn't pick up how it works.
let name = "Steve";
function sayHelloOne() {
console.log(`Hello, I'm ${this.name}`);
}
sayHelloOne();
This question already has answers here:
Constructor behaving differently using ES6 shorthand notation
(3 answers)
TypeError: function is not a constructor (evaluating 'new self.f(1)')
(1 answer)
What is the difference between these object literals?
(1 answer)
Closed 5 months ago.
Regardless of intent, it's unclear to me why one is valid and other other is not. If someone could point to the relevant part of the language spec, that would be especially helpful.
class Foo {
bar() {}
}
Foo.prototype.baz = function() {};
const f = new Foo();
new f.baz(); // Valid
new f.bar(); // invalid, throws error
As pointed out in the comments on the original question, the spec defines it this way. A constructor must have the internal [[Construct]]s, and while ordinary function expressions do get this slot set, method definitions do not. This is described in MDN (as another commenter pointed out) as well.
This question already has answers here:
How does this object method definition work without the "function" keyword?
(2 answers)
No colon after property name in object declaration, is it valid? [duplicate]
(2 answers)
Closed 7 months ago.
Could I please have an explanation on why returning an object of functions without specifying a key-value pair is valid? From my understanding of objects in JavaScript, objects must have key-value pairs, but the object being returned below does not.
function returnFunc() {
return {
printHello() {
console.log('Hello');
},
printBye() {
console.log('Bye');
}
}
}
returnFunc().printHello();
And if I try to return a literal without specifying a key-value pair, I get an error as expected.
function returnLiteral() {
return {
'Hello',
'Bye'
}
} // THIS FUNCTION IS NOT VALID
This question already has answers here:
How does this object method definition work without the "function" keyword?
(2 answers)
ES6 Object Literal Syntax for Methods
(1 answer)
Closed 2 years ago.
I have code that has the following functions written:
testing: function() {
///
}
what is the difference of writing it as :
testing() {
///
}
Do they mean the same thing and have the same purpose?
This question already has answers here:
What does curly brackets in the `var { ... } = ...` statements do?
(4 answers)
Closed 4 years ago.
I upgraded a npm and now my webpack fails around this line here.
const { theme } = params;
What does this mean when you have { } around the variable name?
This is called destructuring assignment. The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.