0

I was playing around with Javascript with this code snippet:

function personDetails(age) {
  var param = "The age of " + this.name + ' is ' + age;
  return param;
}
var details = personDetails(26);
console.log(details);

'this' is not defined for the function. (yes the function should be used with call and apply methods).

But I am curious about the fact that, I am getting different outputs when I run it in terminal, browser(loading via a .html file) and fiddle.

Running in terminal using node gives the o/p: The age of undefined is 26.

Running in browser(loading via a .html file): The age of is 26.

Running in Fiddle: The age of result is 26.

I am curious about why "undefined" in terminal, why nothing in browser and why "result" in fiddle.

atish
  • 355
  • 2
  • 13
  • You need to learn how to use `this` in javascript. Also, you need to use `new` when using this. – evolutionxbox Jun 20 '17 at 13:27
  • Also look at https://stackoverflow.com/questions/15831509/javascript-this-refers-to-window-instead-of-object-inside-function – evolutionxbox Jun 20 '17 at 13:27
  • @deceze I don't think that's the best dupe target. As the OP doesn't know how to use `this`. – evolutionxbox Jun 20 '17 at 13:29
  • @evo How do you know? Sounds to me like OP is very aware of how `this` works. (First sentence under the code snippet.) – deceze Jun 20 '17 at 13:30
  • 2
    @deceze requesting reopen since the """duplicate""" doesn't explain why this.name has different outputs in different environments. – Tom M Jun 20 '17 at 13:31
  • @deceze that's due to the rest of OP's question is towards `this` (not `name`), and why it's different in each env. – evolutionxbox Jun 20 '17 at 13:32
  • 2
    @TomM — Umm. The duplicate does explain that. – Quentin Jun 20 '17 at 13:33
  • 2
    I don't think that's a dupe. The OP wants to know the difference between three different contexts of execution (terminal, browser and fiddle) – mattias Jun 20 '17 at 13:36
  • @deceze : Thanks for the links. I got it. this refers to "window" in browser and in Node.js all modules (script files) are executed in their own closure. But what about the "result" in fiddle. Why is an undefined "this" being output as "result". – atish Jun 20 '17 at 13:37
  • 1
    @Quentin the answer explains that `this` is determined by context and thus refers to the window object in this specific case. The question is about why there are these three specific outputs. In my opinion people are acting too rash, closing threads with reasonable questions. This is not constructive. – Tom M Jun 20 '17 at 13:38
  • 2
    @AtishRaina because this in javascript is context sensitive. In each example the context is different: - In the terminal `this` is the `global` object and `global.name` is `undefined` - In the browser `this` is the `window` object and `window.name` is an empty string - In fiddle `this` is the `window` object also but it has a property `name` with `result` as a value. – mattias Jun 20 '17 at 13:38
  • 1
    @Matias: Thanks a lot. That explains it indeed. – atish Jun 20 '17 at 13:41
  • 1
    @Matías — The difference between the browser and node is explained by "The 'this' keyword behaves differently in Nodejs and browser ". The difference between the two different browser results on different pages is explained by "JavaScript predefined variable “name” ". The dupe targets are fine. – Quentin Jun 20 '17 at 13:59

0 Answers0