what is meaning of "this" in this piece of code? [duplicate] - javascript

This question already has answers here:
this inside function
(6 answers)
Closed 3 years ago.
$("btn").click(function(){
function(){
console.log(this);
}
})
Hi All,
where "this" is refering in this piece of code?A interviewer asked me,i got confused.Please suggest.Thanks

this keyword refers to an object, that object which is executing the current bit of javascript code.
In other words, every javascript function while executing has a reference to its current execution context, called this. Execution context means here is how the function is called.
To understand this keyword, only we need to know how, when and from where the function is called, does not matter how and where function is declared or defined.
function bike() {
console.log(this.name);
}
var name = "Ninja";
var obj1 = { name: "Pulsar", bike: bike };
var obj2 = { name: "Gixxer", bike: bike };
bike(); // "Ninja"
obj1.bike(); // "Pulsar"
obj2.bike(); // "Gixxer"

Related

Calling one function with another function variables javascript [duplicate]

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Learning JavaScript: Lexical Versus Dynamic Scoping
(3 answers)
What is lexical scope?
(21 answers)
Closed 8 months ago.
I am trying to learn JS internals.I am currently trying to print message "yo" both times using the two console.logs. I tried using call,bind or apply to see if it will help but it still doesn't. On the execution stack it places func2 execution context above the func1 execution context. So, I was hoping it would take the func2.
var a = 'hello';
function func1() {
console.log(a);
}
function func2() {
var a = 'yo';
func1();
console.log(a);
}
func2();
I know that if we put it as a nested function it will work.
var a = 'hello';
function func2() {
var a = 'yo';
function func1() {
console.log(a);
}
func1();
console.log(a);
}
func2();
Is there a way to print the internal message without declaring a function inside like the 2nd example without passing a variable to func1?
I think the reason why the first is not working is because this refers to the window but slightly confused due to Execution Context placed over the Execution Stack.

Why does a function not have access to the properties of the object in which it was created? [duplicate]

This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
How to access the correct `this` inside a callback
(13 answers)
Closed 1 year ago.
I was going through this article and couldn't understand their solution 1 to losing context:
let user = {
firstName: "John",
sayHi() {
alert(`Hello, ${this.firstName}!`);
}
};
// Problem:
setTimeout(user.sayHi, 1000); // Hello, undefined!
// Solution 1:
setTimeout(function() {
user.sayHi(); // Hello, John!
}, 1000);
In the second case, I understand that this works because the anonymous function callback gets access to a lexical environment populated with the globals (here, user) but then even user.sayHi() should have a similar lexical environment with access to the stuff between the surrounding {} (i.e. firstName) so we shouldn't need the wrapper in the first place. What am I missing here?

Javascript variable scope in anonymous function [duplicate]

This question already has answers here:
How do JavaScript closures work?
(86 answers)
Closed 3 years ago.
I'm confused as to why the below will output "1".
If I use var to declare a in myFunc that should limit the scope of a to that function.
Calling "a" in the anonymous function (in the setInterval call) is a different function so why is that considered with the scope of myFunc?
Is it only because the function is anonymous? I would expect a to be available to another function unless bind() was used.
myFunc = function(){
var a = 1;
var int = setInterval(function () {
console.log(a);
}, 5);
}
The function bound to setInterval is a closure, when something is unknown in its own scope, it looks in the outer scope, in your case myFunc scope

Outside scope this key word in JavaScript? [duplicate]

This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed 5 years ago.
I am not sure why I'm getting undefined with the code below. I tried to declare a variable under the say method var _this = this and then console.log out _this.name but it did not work.
let dog = {
name: 'doggo',
sayName() {
console.log(this.name)
}
}
let sayName = dog.sayName
sayName()
window.name="test";
sayName();//test
Executes the function in window context so this is window. You may want to keep the dog context either through passing it:
sayName.call(dog);//doggo
Or through keeping a bound function:
let sayName = dog.sayName.bind(dog);
sayName();//doggo

Can you explain this weird function declaration behavior? [duplicate]

This question already has an answer here:
Function call javascript [duplicate]
(1 answer)
Closed 6 years ago.
Can you explain this?
var guessWhat = function(){ console.log('Print this!!!'); };
function guessWhat(){ console.log('Print that???'); }
guessWhat();
// output: Print this!!!
Both are declared on the global scope. Why is the the second line not overriding the first? Is the second function lost in limbo?
function guessWhat(){ console.log('Print that???'); } // declaration
This is a function declaration, it is defined before any code is executed.
var guessWhat = function(){ console.log('Print this!!!'); }; // literal
This is a function literal, it is defined at run-time.
so, the function definition gets loaded first (before any code), and the function literal afterwards, which overrides the first definition, hence this behaviour.
Read more here.

Categories

Resources