This question already has answers here:
why this inside function refers to global object in node when function itself is not defined on global?
(1 answer)
Why is "this" in an anonymous function undefined when using strict?
(3 answers)
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed last month.
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.
Related
This question already has answers here:
ES6 arrow functions not working on the prototype?
(4 answers)
What does "this" refer to in arrow functions in ES6?
(10 answers)
Closed 9 months ago.
I would like to write my own function for every array and I'm using the prototype keyword.
The problem is that when I created the following script:
Array.prototype.test = () => {console.log('length: ',this.length)};
const result = [1,2,3].test();
If I run it in the console of chrome I get the following result:
length: 0
If I run it on my local machine with node script.js I get the following result:
length: undefined
Why is this.length different and why it doesn't reflect the length of the array I'm calling the function on?
That's because you are using the arrow function syntax. The arrow function does not possess a this context, so it "points" to the global context.
The solution is simple:
Array.prototype.test = function() {console.log('length: ', this.length)};
const result = [1,2,3].test(); // length: 3
By adding explicitly function now your context (this) points to the object calling the function.
You are using an arrow function syntax.
When the "this" keyword are being called, it falls back into the global object.
In the browser it falls back into the window object and for this 😉 reason it's return 0 length.
But in the global node object, the length is undefined.
Instead, use the function keyword to get the Array.prototype context.
Array.prototype.test = function(){console.log('length: ',this.length)};
This question already has answers here:
What is the purpose of calling (0, func)() in JS? [duplicate]
(1 answer)
Why does babel rewrite imported function call to (0, fn)(...)?
(3 answers)
Closed last year.
I'm looking at some javascript code that does this:
const senderPubKey = (0, secp256k1_1.ecdsaRecover)(signature, recovery.toNumber(), msgHash);
I don't understand this syntax. When I do console.log(secp256k1_1.ecdsaRecover) I get [Function: ecdsaRecover] so I'm guessing it's a function call but why not do secp256k1_1.ecdsaRecover(signature, recovery.toNumber(), msgHash)?
This question already has answers here:
Lint warning for invoking an async function without `.then()` or `await` without TypeScript
(1 answer)
How to warn when you forget to `await` an async function in Javascript?
(3 answers)
Check if a js function is called using await?
(2 answers)
Making new type of errors in JavaScript
(1 answer)
JavaScript / Mocha - How to test if function call was awaited
(3 answers)
Closed 2 years ago.
Basically had an async function in JavaScript (nodejs app) that returned a list...unfortunately I forgot to call await on this testFunction(x, y) and so it broke some code in production. Is there a linting rule that would catch this? Is there a test I can write to ensure that this won't break because I forgot to call await (it seems like linting is the way to ensure errors like this are caught)?
const testFunction = async (x, y) => {
let z = await someFunction();
//..some code that returns a list after processing z
}
This question already has answers here:
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
React - uncaught TypeError: Cannot read property 'setState' of undefined
(20 answers)
Closed 3 years ago.
I need to convert an arrow function into a function. This is due to a problem with Slick Slider, as it does not support properly arrow functions.
I have convert this:
beforeChange={(current, next) => this.setState({slideIndex: next})}
Into this:
beforeChange={function(current, next) {
this.setState({slideIndex: next});
}}
But does not work. Already tried this solution:
beforeChange={function(current, next) {
this.setState(function() {
return {slideIndex: next};
});
}}
Use bind for passing the context to the function:
beforeChange={function(current, next) {
this.setState({slideIndex: next});
}.bind(this)}
This will pass this to the function so that when referencing this inside the function will be the one passed through bind.
Arrow functions do this automatically, they pass the context of where they're being called to the function itself, so by removing the arrow function you removed the correct context.
Other functions that will allow you to manually pass the context are: apply, call and the aforementioned bind.
This question already has answers here:
In what scope are module variables stored in node.js?
(4 answers)
Why does a module level return statement work in Node.js?
(1 answer)
How to change the Node.js module wrapper?
(3 answers)
Closed 3 years ago.
argument.length always returns 5 while gives an error in browser
I know that arrow functions does not have arguments then why it is returning 5.
t =()=>{
console.log(arguments.length)
}
t();
arguments.length should have return error or 0
The arrow function returns 5 because of using node,an arrow function inside a regular function
Arrow functions don't really have an arguments object.You can refer this for more information Official information on `arguments` in ES6 Arrow functions?
But why does it always return 5 and not undefined??
The reason behind this is in node your arrow function is wrapped around in a regular function while using the node command. And as regular functions have arguments this argument is being used by your arrow function.
When you execute your arrow function it does not find arguments in its execution context and goes a level up to find it and as your function is wrapped by a normal function by node it finds the arguments object and uses it.
Although this behavior will only be shown in node and not in browser as it will not have arguments in any execution context.
The reason why 5 is being returned is because the 5 arguments are (exports, require, module, __filename, __dirname) and an easy way of confirming this is make an error in your on the very first line of the JS file.