How to get the scope from where a function was called? [duplicate] - javascript

This question already has answers here:
How to access values from <function scope>'s Closure in Chrome Developer tool's Watch panel?
(2 answers)
What way (if any) can I get the reference to anonymous function's closure in javascript?
(3 answers)
Is it possible to gain access to the closure of a function?
(4 answers)
Closed 5 years ago.
I'm trying to create a javascript expression parser, able to parse an expression and get the declared variables in its body, without executing the function:
function test(expressionFn) {
var expression = getExpression(expressionFn);
// get value expression.expressionRight in scope
// not work -> var valueRigthInScope = expressionFn.prototype.constructor[[[Scopes]]][expression.expressionRight];
console.log(expressionFn.prototype); // see ".constructor[[[Scopes]]]" in debug tools [F12]
}
function getExpression(expressionFn) {
var strAfterReturn = expressionFn.toString().split("return")[1].trim();
let strExpression = strAfterReturn.split(";")[0].split(" ");
return {
expressionLeft: strExpression[0],
operator: strExpression[1],
expressionRight: strExpression[2]
};
}
function start(){
var myValue = "This is value!";
test(function(x) { return x.prop1 == myValue; });
}
start();
<h1>See console</h1>
Example in JsFiddle.
But I can not access the scope of the function to get the value of the variable myValue, for example.
In the Google Chrome console I managed through the function to get the scope where the variable myValue is declared, but I can not access that same scope in javascript.
Follows the image of the Google Chrome console:
How can I access [[Scopes]] in the image?

Related

Using outside variables inside an arrow function in Node.js [duplicate]

This question already has answers here:
How can I pass variable into an evaluate function?
(7 answers)
How do i return a value from page.evaluate() in puppeteer?
(3 answers)
Closed 1 year ago.
I'm kind of new to JavaScript but I'm using a function that takes another arrow function as a parameter. All I need to do in the function is simply set a value to some variable I have declared before it. From my understanding, var means it has global scope, so I'm not really sure why I cant use the variable. If I try to pass it in the parameter I get undefined. I'll paste the 2 lines of code giving me issues
var thing = events[i]["Address"];
await page.evaluate( () => document.querySelector('[class="form-control tt-input"]').value = thing)

Access local variable from the global context? [duplicate]

This question already has answers here:
What is a 'Closure'?
(22 answers)
How do JavaScript closures work?
(86 answers)
Closed 5 years ago.
function wrapValue(n) {
var localVariable = n;
return function() { return localVariable; };
}
var wrap1 = wrapValue(1);
var wrap2 = wrapValue(2);
console.log(wrap1());
// → 1
console.log(wrap2());
// → 2
In this example how is it allowed to access localVariable, which is local to wrapValue() function, from the global environment?
The book, included this example, stated the reason as follows:
"multiple instances of the variable can be alive at the same time".
But I didn't understand.
Your function sets a private value (localVariable) when called and returns it immediately so you can assign it to a another variable without change the private var inside function.

Javascript Closure -Local variable nested function [duplicate]

This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 6 years ago.
I am trying to use a variable x defined inside a function P whose value I am trying to set in another function. It always comes undefined.
I tried applying my mind to use closure but it's just going off my head. It does not gives me a string rather a object.
The logic is as follows.
function P(i){
var x;
if(i!=null){
//this pulls the data correctly and i could see it in network tab response.
var dataFromQuery=widgets.DATA.create({
url:"abc/cde",
queryTemplate :"/query"+i+ "?"
});
//we query the data and set the value as per the logic.
dataFromQuery.query(function(data){
if(data[0].name){
x=data[0].name; // this stays undefined , and i understand this is a new local variable x.Also the value is here and comes fine here so no issues with the data but i want to set it to the variable defined outside as x.
}
})
}
else{
x="somehardcode";
}
};
I have tried storing the result dataFromQuery.query(function(data){ to a var and then setting it to x but it again comes as a object, which i need as a string.
Thanks
I think you're looking for something like this:
var P = (function() {
var x;
function _P(i) {
//your internal logic here
}
return _P;
})();
Both xand _P are wrapped in the enclosure and scoped only to the auto-executed anonymous function. _P is returned and available as var P in the outer scope, but x will remain hidden.

Access closure variable [duplicate]

This question already has answers here:
How to access javascript variable stored in function closure from browser console?
(2 answers)
Closed 9 years ago.
I have a feeling I know the answer to this question, however is there anyway to view the current value of inside via console?
var test = (function() {
var inside = 0;
return function() {
inside++;
console.log(inside);
return inside;
}
})();
Just call test() and console.log will print the value.
Your closure is self invoked but your inner method is just returned so it is never called in your example.
You should use:
var test = (function() {
var inside = 0;
return function() {
inside++;
console.log(inside);
return inside;
}
})();
test();//Will print 1
So it will create the closure and your return statement will put the inner function in the variable test. So your test became the inner function. Then you need to call it.

Javascript: this.varName = "" vs var varName = "" [duplicate]

This question already has answers here:
Difference between this and var in a function
(5 answers)
What is the scope of variables in JavaScript?
(27 answers)
Closed 9 years ago.
I'm new to JS so noob question:
I see functions which define vars inside them:
function functionName(){
this.something = "";
};
If I understand correctly, something is a local variable? Why is it being defined as this.something = '' as opposed to var something = ''?
Is there any difference, and if so what is it?
It sets the attribute something of this. What this refers to depends on how functionName is invoked, but typically it's an object created with new:
var foo = new functionName();
console.log(foo.something);
If you'd use var something inside the function, the variable would be inaccessible from outside, so you couldn't do foo.something in the above example.
var someThing
is a local variable - meaning it exists within the scope of the current block
this.someThing
is an instance variable - meaning it belongs to the object and is visible in all methods of that object.

Categories

Resources