Differences in Strict mode vs. Non-strict mode

First off, the This function always refers to an object. It’s value being completely dependent on how a function is called in Javascript. Knowing this it may be apparent that when using strict mode as opposed to non-strict mode the way This works will also change.

Strict mode makes several changes to normal JavaScript semantics. First, strict mode eliminates some JavaScript silent errors by changing them to throw errors. Second, strict mode fixes mistakes that make it difficult for JavaScript engines to perform optimizations: strict mode code can sometimes be made to run faster than identical code that’s not strict mode. Third, strict mode prohibits some syntax likely to be defined in future versions of ECMAScript.

In the global execution context (outside of any function), this refers to the global object, whether in strict mode or not.

In functions where the values are not defined, using this and not using strict. This is the object itself. However when using strict and this where the value of a function has not been set. Instead of the result being the object the result is undefined.


 

This  basics

When a function is called as a method of an object, its this is set to the object the method is called on. The this always refers to and contains the value of the object and it is usually used inside a function.

This keyword  is not assigned a value until an object invokes the function where this is defined. The value This is assigned is based only on the object that invokes the this Function. this has the value of the invoking object in most circumstances. However, there are a few scenarios where this does not have the value of the invoking object. I touch on those scenarios later.

 

Sources on This

This with Clarity- Javascript is Sexy

What does this mean? – StackOverflow

This – JavaScript | MDN

 

 

Advertisements