As web developers, we use Javascript in our daily life but rarely do we take out the time and learn intricacies of the language. We accept many things at face value as they do not impact the work we need to do and make assumptions about other things that are mostly correct.
But once you know you will be working on something for a long time, it’s time to put the foot down and start learn things properly to qualify from development to engineering.
Below are some topics that will help you get an overview of the Whats and the Hows:
Scopes
- Scope determines the visibility of variables, functions, and objects in some particular part of your code during runtime.
- Also called Execution Context (Not to be confused with Context below)
- Types of Scopes:
- Local - Variables defined inside a function are not available outside that function
- Global - Variables defined outside any scope and can be accessed anywhere in the code
- Lexical - Variables from parent scope are available inside the nested scopes.
- Scope Chain: When accessing a variable, JS will start searching from the current scope, and keep jumping up in nested scopes to find it.
Context &
this
keyword- Developers generally get confused between scope and context. Context is used to refer to the value of
this
variable in some place in the code. - Generally,
this
keyword in a context refers to the object it was called against, i.e. the variable left of the dot in a function call.
- Developers generally get confused between scope and context. Context is used to refer to the value of
Closures
- It is created when an inner function tries to access the variables defined in and arguments passed into the outer function.
- Even when the inner function is called outside the outer function, those variables will be available to the closure.
Constructors
- These are the functions to the right of class definition.
- The constructor function is nothing more than a normal function.
Prototype
- This is a default attribute in every javascript function.
- It points to an object in which properties and functions of a class can be assigned that should be usable by every object of that class(object of the constructor function).
- Every object has a prototype that can be modified through the constructor’s prototype.
- If the current scope doesn’t have the accessed attribute, the prototype of that function is checked before going to outer nesting.
- Every prototype has a
constructor
property that points to the class(Constructor function) of that prototype.
Prototype Chain
- Every object has a prototype which has been chained all the way upto
Object
variable.
- Every object has a prototype which has been chained all the way upto
References
Object-Oriented JavaScript - Udacity
Understanding Scope in JavaScript - Scotch.io
Understanding the JavaScript Prototype Chain & Inheritance - RisingStack