Why JavaScript is not implied

In JavaScript, this should always be specified explicitly when accessing its properties. For example:

 function Frobber(x) { this.x = x; return this; } Frobber.prototype.frob = function () { // wrong: return x * x; // right: return this.x * this.x; } 

I know that I can use with(this) (which is deprecated and generally not approved), but why aren't this properties in the scope automatically? I think there must be a reason for making such a constructive decision.

+12
javascript language-design
Feb 02 2018-11-18T00:
source share
6 answers

This is similar to Python. The reason is quite simple: you cannot add this because it encounters a rule to define a search scope for searching non-local variables in external areas. This is possible in statically typed languages, since this members are known at compile time.

And what if it made this dynamic decision, for example, " x refers to this.x if this.x !== undefined and otherwise to the variable x " (or any other rule for this that is defined at run time) ? This is very dangerous, because it can shade local variables based on what this happens, that is, it breaks perfectly valid code only for certain objects. Another problem: should undeclaredVar = ... add a new instance attribute? If not, it will be the ugly asymmetry between implicit and explicit this . If it creates an instance attribute, you lose the ability to set a global and trailing variable from within the functions β€” not too many losses, many will say; but the JS developers seemed to think differently, because by default they chose a global scope.

Creating attributes of a shadow instance of "random variables" would be less dangerous, but with deeply nested areas filled with many names, in most cases you would have to use this. , therefore, fewer wins on the network. For this and / or perhaps for other reasons, the designers considered that reduction was impossible.

+9
Feb 02 2018-11-18T00:
source share

What "this" means in Javascript is completely a function of how the current function was called.

If it is called as a method (i.e. with an operator.), Then 'this' will be set to the object before the point.

If it is called as a simple function, then 'this' will be a global object (usually this is a window).

IIRC, if you use Function.call, you can set it explicitly.

Javascript is an OO language, but has no classes and no (strictly) methods.

+2
Feb 02 2018-11-18T00:
source share

If x meant as this.x , how would you refer to variables defined as the name x ?

 function Frobber(x) { this.x = x; } Frobber.prototype.frob = function() { var x = 'WHAT ABOUT ME'; return x; } 
+1
Feb 02 '11 at 15:08
source share

You must explicitly specify "this" because instead, "window" is implied

the code

 function SomeFunc(x) { y = x; return this; } 

Same as

 function SomeFunc(x) { window.y = x; return this; } 
+1
02 Feb 2018-11-11T00:
source share

Each function object introduces a new namespace and populates it with its parameters, variables, and declarations of the internal function. If all the properties of the this object were to be entered into the same namespace, the names would collide.

To prevent this collision, you must explicitly ensure that each parameter name, local variable name, or function declaration name does not match any of the properties of the object referenced by this .

Since you can dynamically add new objects to an object, you also need to make sure that any added new property does not collide with all of these parameters, variables, and ... in each of the methods of the objects.

It would be almost impossible to handle.

+1
Feb 02 2018-11-11T00:
source share

From JavaScript: Good Parts ( Functions - Invocation ):

There are four call patterns in JavaScript: a method call pattern, a function call pattern, a constructor call pattern, and an application call pattern.

Basically, each of these "patterns" defines a way to define the this link. If the function is defined as an object method, this will refer to the parent object. If the function is not a property of the object, this refers to the global object. If the function is called with the new keyword (i.e., as a constructor), then this refers to the newly created object. And finally, if you use the apply () function, the this link is bound to any object you specified.

0
Feb 02 2018-11-18T00:
source share



All Articles