Answer the question
In order to leave comments, you need to log in
How to make scope in javascript?
Hey!
I'm not sure many people know the answer to this question.
The function defines its own scope - this is a certain object , the properties of which are the variables defined in the function.
Question: is it possible to refer to this object ?
Otherwise, is there access to full scope control?
Thank you!
PS inspired by this habrahabr.ru/qa/19385/
Answer the question
In order to leave comments, you need to log in
You can only access the global object, through window, self or this (the latter is for a function - not an object method). Even for internal functions with their own scope, this will be window.
<script>
(function(){
var aa = function(){
console.log(window==this, this)
}
aa()
})()
</script>
Local scope is not available. It was done, apparently, for the possibility of writing independent scripts on one page or, in other words, for another script not to influence the first one through the scope in any way (for example, to erase variables from it).
There is no way you can access the list of external visible variables just by knowing their name, or if the function is at the top level, then you can override window.
Maybe I didn't quite understand the question, but...
var data = (function(){
var a = "provate string";
var b = "public string;";
return {
a: a
};
})();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question