How does this work in a standalone function? [duplicate] - javascript

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

Related

In JavaScript what is this method called where you might define a variable or property as such variable_name$ref [duplicate]

This question already has answers here:
Rules for unquoted JavaScript Object Literal Keys?
(6 answers)
What characters are valid for JavaScript variable names?
(12 answers)
Closed 1 year ago.
I am seeing it crop up more and more in code I am going through on a new project (can't share due to contractual reasons) where Ill see something like:
{
prop1: value$ref,
$prop2: null
}
I have see ${prop3} before, but never an example without the brackets. Can anyone provide direction as to what the method is, or the operator is or whatever the case?

What is this syntax for creating an object? [duplicate]

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.

Losing class context when calling function by reference [duplicate]

This question already has answers here:
Javascript lost context when assigned to other variable
(4 answers)
Class methods assigned to variable (alias) lose the value of this in Javascript [duplicate]
(2 answers)
How to access the correct `this` inside a callback
(13 answers)
Why is JavaScript bind() necessary?
(4 answers)
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed 3 years ago.
I've got a library that makes api calls to a number of sub-api's. If I call a function directly all works great:
let result = await apiLib.subApi1.getFoo();
But if I call by reference, I seem to lose the class context (i.e. 'this' is undefined within the class)
const endPoint = 'Foo';
let apiLibCall = apiLib.subApi1['get' + endPoint];
let result = await apiLibCall();
Is it possibly because of how 'await' works? Or am I calling by reference incorrectly?

What is the value of this inside object literal??why pointing to window object? [duplicate]

This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
How does the "this" keyword in Javascript act within an object literal? [duplicate]
(4 answers)
Closed 4 years ago.
I am new to js and not able to understand which this does not points to the "obj" in the below code:
var obj = { x:this };
obj.x; // window object
I have referred many sites but all states the this always points to the object in whose context the method was called..does that not hold true for property??
Also why this pointing becomes correct inside a function as property of same object?? I have read that scope and context are not to be confused are different, then why only function is creating right context??I read that scope are of two type local and global then why context is affected.
Please explain in details, this question might be irrelevant maybe because i am still confused in scope and context..shares useful links too.

What is the difference between a simple function and a function with this keyword in javascript? [duplicate]

This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
What is the 'new' keyword in JavaScript?
(17 answers)
Closed 5 years ago.
I have the following code-
var x=function(){
alert("hello");
}
var z=x;
z();// it works nicely
Now say I modified the code a bit-
var x=function(){
this.show=function(){
alert("hello");
}
}
var z=x;// It doesn't work unless I write var z=new x();
z.show();
I am wondering what difference 'this' keyword is creating between this two function and why I need to use an extra new keyword in the second case.

Categories

Resources