0

Below Node.js code if I run that on other places it works well, but in VS Code it throws an error.

    this.k= 8             
    TypeError: Cannot set properties of undefined (setting 'k')
    at subfun (file:///c:/Users/Administrator/Documents/2.js:3:15)
    at mainfun (file:///c:/Users/Administrator/Documents/2.js:6:5)
    at file:///c:/Users/Administrator/Documents/2.js:8:1
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:530:24)
    at async loadESM (node:internal/process/esm_loader:91:5)
    at async handleMainPromise (node:internal/modules/run_main:65:12)

My code:

var mainfun = function (){
    function subfun(a){
        this.k= 8
        console.log(this.k)
    }
    subfun()
}
mainfun()

Need the above solution to run on VS Code and I'm looking for an error reason.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
joren
  • 1
  • 4
  • You are most likely in strict mode, and that is why are you getting the error. In non strict mode k would be created as a global variable but in the strict mode, you will receive an error. Your code does run. – Tamir Abutbul Jan 20 '23 at 14:47
  • @TamirAbutbul how to run in non strict mode ?? – joren Jan 20 '23 at 14:56
  • @joren Once enabled, strict mode cannot be disabled. That’s the wrong question to ask, anyway. `this.k = 8` is incorrect code in this context and strict mode nudges you to write more correct code. What is `this` _supposed_ to refer to? Why do you believe you need to use `this` here? – Sebastian Simon Jan 20 '23 at 14:59
  • Actually subfun is a [constructor function](https://rollbar.com/blog/javascript-constructors/#:~:text=A%20constructor%20is%20a%20special,for%20any%20existing%20object%20properties.). Check [this stackblitz snippet](https://stackblitz.com/edit/node-osflrj?file=index.js) to see how it can be used. – KooiInc Jan 20 '23 at 15:00
  • @SebastianSimon for my situation and condition on code i used like that . And i think that's not wrong too. Atleast let me know on even new project how to disable strict mode ? – joren Jan 20 '23 at 15:18
  • @KooiInc thanks for reference code. It will help me . But there as any other easy and simple to write it ?? – joren Jan 20 '23 at 15:19
  • @joren There is no reason _ever_ to disable strict mode. Please don’t fight against good practices. `this.k` is meaningless in this context, so it _is_ wrong. Why do you believe it’s not wrong? What is `this` supposed to be? – Sebastian Simon Jan 20 '23 at 15:29
  • @joren: see the modified code in the [snippet](https://stackblitz.com/edit/node-osflrj?file=index.js). – KooiInc Jan 20 '23 at 15:38
  • @SebastianSimon anyways im buliding the more nested functions and calling over vars on it. So this function will help me lot . So final words disable as not possible?? – joren Jan 20 '23 at 15:48
  • @joren _“calling over vars on it”_ — What does this mean? Keep in mind that `k` is not a variable in your code, but a property. As for strict mode, please read the [documentation](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Strict_mode) and familiarize yourself with how it becomes enabled and if it’s possible to disable it. – Sebastian Simon Jan 20 '23 at 15:51
  • @SebastianSimon in doc there not mentioned clearly about making sloppy mode !! Let me know if you if you know – joren Jan 20 '23 at 16:08

0 Answers0