0

I have some simple code here that I'm running in Visual Studio Code with Quokka and NodeJS.

var str = "hello"

function printStr(){
    console.log(this.str);
}

printStr();

Output:

undefined​​​​​ at ​​​this.str​​​ ​quokka.js:6:4​

I can run this code in my web browser just fine, and it works just fine, printing out "hello".

"use strict"; is not enabled

Screenshot: https://i.imgur.com/IEQwv5D.png

Kevin B
  • 92,700
  • 15
  • 158
  • 170
Vladz0r
  • 27
  • 3

3 Answers3

1

In a browser this will be interpreted as the window object in this case and the variable str will be defined on the window. There is no window object in Node. It's not clear why you are using this at all here rather than using regular scoping rules. This will work in both the browser and Node:

var str = "hello"

function printStr(){
    console.log(str); // will see outside scope
}

printStr();

Better yet, pass the value into the function so it doesn't depend on values defined outside its scope:

var str = "hello"

function printStr(s){
    console.log(s);
}

printStr(str);

In Node there is a global object, which has some similarity to the browser's window object so code like this can work in Node, but it would be a fairly non-standard way to do this:

global.str = "hello"

function printStr(){
    console.log(this.str)  
}

printStr();
Mark
  • 74,559
  • 4
  • 81
  • 117
  • _"There is no window object in Node"_. But there is a `global` object that works much the same way. This snippet actually does work in node, just not in this vscode js runner. (Though this is still a very bad practice) – Alex Wayne Oct 16 '18 at 18:17
  • *"There is no window object in Node."* That's right, but there is `global`. More importantly, in Node the code is likely evaluated as a module. `this` inside modules refer to `module.exports`. Variable declarations do not magically become properties of `module.exports` (also relevant: https://stackoverflow.com/questions/22770299/meaning-of-this-in-node-js-modules-and-functions) – Felix Kling Oct 16 '18 at 18:17
  • Ah. I found this type of example in the book "You Don't Know JS" and was trying to test it out and understand it. I still have a lot to learn about [this]. – Vladz0r Oct 16 '18 at 18:20
  • @AlexWayne the `global` object in Node is much different. Importantly in this case the variable `str` won't be defined on it and `this` won't point to it in the function. – Mark Oct 16 '18 at 18:20
  • I'm not sure I understand your point about `global` @FelixKling — the `global` object doesn't seem relevant here since `var str` won't be a property of it and `this` in the function won't refer to it. – Mark Oct 16 '18 at 18:22
  • @MarkMeyer it does work though `node -e 'var str = "hello"; function printStr() { console.log(this.str); }; printStr(); //-> hello` and `node -e 'console.log(this === global); //-> true` Though this may be different when executing a file on disk. – Alex Wayne Oct 16 '18 at 18:25
  • @AlexWayne evaluating on the command line is an edge case. Almost **all** realistic situations will run node from a file which will then be a module. Put the code in a file and run it (which is how almost all node code is run) and it won't work. – Mark Oct 16 '18 at 18:29
  • My point is: There is a global object in Node (doesn't matter whether it's called `window` or `global`), so one might ask why is the variable not a property of the global object when the code is executed in Node? The answer to that is module scope, which you didn't mention in your answer, but seems highly relevant. (I did get one thing wrong: `this` inside the function would of course refer to `global`, not `module.exports`). – Felix Kling Oct 16 '18 at 18:48
  • Completely off-topic (will delete in a bit): I like your portfolio :) I just recently have been to Alaska for the first time (though not as far north as Anchorage) and really liked it. – Felix Kling Oct 16 '18 at 18:52
  • Thanks @FelixKling, if you ever end up in Anchorage, feel free to drop me a line. There's a small but active coding community up here and a lot of stuff to see within a day's drive. – Mark Oct 16 '18 at 18:55
0

Inside a function this normally refers to the window object and the variable str is not defined on window.

You may simple call it like

var str = "hello"
function printStr(){
    console.log(str);
}
printStr();
brk
  • 43,022
  • 4
  • 37
  • 61
0

I hope mi answer will be to help. The object 'this' is undefined in node JS for the element window not exist and you aren't working with any object or constructor or class.

For example:

var Animal = function(kind) {
    this.name = "NN"
    this.kind = kind    
};

Animal.prototype.printName = function() {console.log(this.name)};
Animal.prototype.setName = function(name){this.name = name}

var a1 = new Animal("Lion");
var a2 = new Animal("Cat");

a1.setName("Robert");
a2.setName("Alex");

a1.printName();
a2.printName();

Please look at the code when I use the sentence this. if you have a questions, please write me! (Y)

Samack77
  • 46
  • 1
  • 4