Javascript code weird output of callback function [duplicate] - javascript

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

Related

Multiple uses of a function in a JavaScript Object [duplicate]

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`

Calling the normal function of "this" [duplicate]

This question already has answers here:
The invocation context (this) of the forEach function call
(5 answers)
Why "this" refers to Window in forEach in javascript? [duplicate]
(8 answers)
Using `this` in a forEach() [duplicate]
(1 answer)
Accessing this in a forEach loop results in undefined
(3 answers)
Closed 2 years ago.
In JavaScript, this depends on the function call method.
Calling General Functions
Method Call
Call constructor function
arrow function call, etc.
but in this case
class Numbers {
numberArray = [];
multiply(arr) {
arr.forEach(function(item) {
this.numberArray.push(item * item);
});
}
}
const numbers = new Numbers();
numbers.multiply([1, 2, 3]);
If you look at the fourth line in this class example,
Since arr.forEach is calling a callback function with this, I think the arr is a this but Why does this means undefined?
I don't know why it's a general function call.

why does `this` point to object [duplicate]

This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed 3 years ago.
In the following code, why test function's second this points to obj, whereas the first this points to window object?
In particular, how can I guess the value of this by looking at a piece of code?
var test = function(){
console.log('first', this)
return function(){
console.log('second', this)
}
}
var obj = {
a: 1,
b: test()
}
obj.b() // execute function
You call test on object creation triggering the first logging and store the created function to the object. You call that function on global level and therefore this gets resolved at that scope.
Is this some sort of interview question?
What are you trying to achieve/understand?
Edit:
See this guide for a good explanation of 'this':
https://dmitripavlutin.com/gentle-explanation-of-this-in-javascript/

Explaining anonymous function [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Advanced JavaScript: Why is this function wrapped in parentheses? [duplicate]
(4 answers)
Closed 4 years ago.
Can anybody explain below code? All I know is it is anonymous function but what is (0) doing?
var output = (function(x) {
delete x;
return x;
})(0);
console.log(output);
Why output of above code comes zero. Can anybody explain?
That's because what you're doing is creating a function and then calling it immediately where x = 0. Your function is returning x, and thus 0.
As for what anonymous functions are, they are basically a function that gets stored in a variable. You call it from the variable instead of by name. So, for example:
var output = function (x) { return x;};
Can be called like this:
output(0);
As opposed to the normal way like this:
function myOutput(x) {
return x;
}
myOutput(0);

What is the difference between these two object methods in Javascript? [duplicate]

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?

Categories