I am new to javascript, why this code return undefined?
const foo = {
bar: function() { return this.baz; },
baz: 1,
};
console.log((function() {
return typeof arguments[0]();
})(foo.bar));
I am new to javascript, why this code return undefined?
const foo = {
bar: function() { return this.baz; },
baz: 1,
};
console.log((function() {
return typeof arguments[0]();
})(foo.bar));
If you are expecting an answer of number
that can not be returned from you code.
This is a misunderstanding of this keyword (as pointed out by @Tushar @Bergi)
The following code does it.
function Foo(){
this.baz = 1;
};
Foo.prototype.bar = function() {
"use strict";
return this.baz;
}
var foo = new Foo();
console.log((function() {
"use strict";
return typeof arguments[0];
})(foo.bar()));