Finding a chain of chains and prototypes is when

If a variable is not available in the function when it is needed, then it is looked up in the scope chain (which is closure), but in other cases it is searched in the prototype chain. I try to wrap my head around what happens when. I was wondering if someone could kindly clear the fog of me or pass me some literature that specifically discusses this topic.

For example, would I say correctly: - Objects and, therefore, public variables bound to the context (this) are always viewed in the prototype chain? - Private variables are always viewed in the scope chain (i.e., Function chains in the execution context)? - Are there any cases when the program looks in both cases?

I tested three different scenarios (chain search, prototype search, and lack of search), but unfortunately it did not help enough to figure it out.

var Food = function(){ var t = 1; // for closure this.timeToPrepare = function(){ // Scope chain lookup console.log(t * 3); }; this.timeToMake = function(){ // This is looked up in the prototype chain console.log(this.t * 3); }; this.timeToEat = function(t){ //No lookup console.log(t * 3); }; }; Food.prototype.t = 2; (function(){ var pizza = new Food; pizza.timeToPrepare(); //3 pizza.timeToMake(); //6 pizza.timeToEat(3); //9 })(); 

enter image description here

Thanks!

+7
javascript closures prototypal-inheritance
source share
1 answer

Variables are scanned in the scope chain, starting with the current execution context and raising the tree that spans the execution contexts.

Properties are checked first on the base object, and then on the object [[Prototoype]] chain (ie, on the internal prototype).

So, if you do:

 foo 

foo will be considered as a variable and will look for a chain of scopes. Variable names are never qualified; you cannot direct them to the specific execution context to look for. If there are two variables in the chain of regions with the same name, you can only access the one that first occurs when the chain moves (there is a way around this especially for global variables), for example

 var a = 'global a'; function foo() { var a = 'local a'; return a; } console.log(foo()); // local a 

In the above, a inside the function resolves the local variable a. In the case of global variables, they become properties of the global object, so you can access them even if they are "obscured" by the same local property name, for example.

 function foo() { var = 'local a'; // this will reference the global object return this.a; } console.log(foo()); // global a 

In contrast, property names are always preceded by the underlying object on which they are viewed (as in the example above, where it refers to a global object), for example

 foo.bar 

will be split into foo and bar. First, foo will be resolved in the scope chain, and if found, resolving the property will try to find the bar property. Therefore, for properties, you can direct the object that the object was looking for. Therefore, if there are two objects with the same named property, you can search for both properties while both objects are in scope.

So, the first part of any link is considered as a variable, the subsequent parts are considered as properties. Except when used, but not recommended. Do not go there.

But for completeness ... with the places of the specified object in the start-up chain of objects, so first the variables are first considered as properties of this object before using the scope chain, so you can do:

 var cos = function(arg){return 'my cos function: ' + arg}; function foo() { // cos is resolved on the scope chain console.log(cos(0.5)); // my cos function: 0.5 with (Math) { // cos is first resolved as a property of Math, and only on the // scope chain if not found there console.log(cos(0.5)) // 0.8775825618903728 } } foo(); 
+14
source share

All Articles