This question already has answers here:
Losing "this" context in JavaScript when passing around members [duplicate]
(3 answers)
How to access the correct `this` inside a callback
(13 answers)
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed last year.
If I use the first representation, the console will log the name, however in the second one it does output "JS Bin Output", any idea what happened?
let person = {
name: 'Fred',
sayMyName: function(params) {
return (this.name);
},
};
let obj = person;
person = null;
let sayMyName = obj.sayMyName;
console.log(obj.sayMyName()); // logs `Fred`
console.log(sayMyName()); // logs `JS Bin Output`
Related
This question already has answers here:
Function context when calling via arguments
(3 answers)
How to access the correct `this` inside a callback
(13 answers)
Closed 1 year ago.
I am not at all able to decode the output for this js code snippet. The output actually shows 3 .
I expected output to be 4. Can someone please explain what is going on here, or am I missing some crude concept of javascript.
var length = 4;
function callback() {
console.log(this.length);
}
const object = {
length: 5,
method() {
arguments[0]();
}
};
object.method(callback, 1, 2);
This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
Methods in ES6 objects: using arrow functions
(6 answers)
How to access the correct `this` inside a callback
(13 answers)
Arrow Functions and This [duplicate]
(5 answers)
Self-references in object literals / initializers
(30 answers)
Closed 1 year ago.
I know that arrow functions don't have their own "this" keyword, and "this" will point to parent (if it has it).
Can someone please explain why "this" in class points to class and why "this" in object points to global window?
So question is about differences between class and object here.
In class:
class App {
getPosition = () => {
//this points to class App
};
getPosition2(){
//this undefined
}
}
Object:
const obj = {
someFunction: () => {
//this points to global window
}
someFunction2(){
//this points to object
}
};
This question already has answers here:
Arrow Functions and This [duplicate]
(5 answers)
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed 3 years ago.
I read this on w3schools that:-
https://www.w3schools.com/js/js_arrow_function.asp
"In regular functions, the 'this' keyword represented the object that called the function, which could be the window, the document, a button or whatever."
"With arrow functions, the 'this' keyword always represents the object that defined the arrow function."
var name = "ABC";
var obj =
{
name: "abc",
func: () => {
console.log(this.name);
}
};
obj.func();
So here, 'this' in the arrow function should be representing 'obj' object, but the output is "ABC", whereas according to me it should be "abc", there should be some concept that I am not aware of right now?
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 6 years ago.
I'm trying to write some JS object. Getting stuck on something, I don't understand how contexts work. Here is a simple example :
var MyApp = function(el) {
this.el = el;
};
MyApp.prototype.bind = function() {
window.setTimeout(this.start, 300);
}
MyApp.prototype.test = function(msg) {
console.log(msg);
}
MyApp.prototype.start = function() {
console.log(this); // Returns the window context
this.test('Hello'); // Doesn't work, of course.
}
var myapp = new MyApp(el);
myapp.bind();
Problem is when calling the start method, I'm in the window context because of the window.setTimeout. Is there a way to fix this or is it a pattern design issue?
Thanks ;)
This question already has answers here:
Use of 'prototype' vs. 'this' in JavaScript?
(15 answers)
Prototyped and a non-prototyped method? [duplicate]
(1 answer)
Closed 8 years ago.
Consider the following code:
function someObj() {
this.someFunction = function() {
console.log('someFunction');
}
}
someObj.prototype.foo = function() {
console.log('foo');
}
I can call both like so:
var test = new someObj();
test.someFunction(); // Logs 'someFunction'
test.foo(); // Logs 'foo'
How would variable scope be affected here? Is there an advantage / disadvantage to each approach?