Skip to content

Instantly share code, notes, and snippets.

@sakshamsaxena
Last active October 13, 2016 14:34
Show Gist options
  • Save sakshamsaxena/be06a4bf3260c320262be72c7dd80b2e to your computer and use it in GitHub Desktop.
Save sakshamsaxena/be06a4bf3260c320262be72c7dd80b2e to your computer and use it in GitHub Desktop.

JavaScript

Closures are special objects or functions that refer to independent variables and/or functions defined in a scope. These keep intact the environment in which they were created.

Normally, the local variables within a function only exist for the duration of that function's execution. However, a Closure retains the function as well as the "environment" (variables in that scope). Thus, the existence of that variable will be accounted for if we're using closures.

Closure definitions returns an accessible quantity like an object (in case of module pattern design) or a function (in case of attaching event listeners dynamically).

Naturally, retention of such an environment is possible when it isn't being changed within itself, using some loops, for example. That would keep the environment inconsistent.

(This is a thick one)

JavaScript is prototype based (which in turn are Objects really, thus very essentially it is Object based). When it comes to inheritance, JavaScript only has one construct: objects. Each object has an internal link to another object called its prototype. That prototype object has a prototype of its own, and so on until an object is reached with null as its prototype. null, by definition, has no prototype, and acts as the final link in this prototype chain.

The properties of any Object are found at different "links" of the prototype chain. Those properties defined at the same links are called "own properties". To check whether an object has a property defined on itself and not somewhere on its prototype chain, it is necessary to use the hasOwnProperty method which all objects inherit from Object.prototype. hasOwnProperty is the only thing in JavaScript which deals with properties and does not traverse the prototype chain.

Property Shadowing and Method Overriding are practices in which the "upper links" redefine the variables/functions used in "lower links", and these new ones then shadow/override the existing ones.

Subsequently, the this keyword in any reference within an object at whatever link will point to the topmost object declaration whenever called.

Objects can be created by either using constructor functions, or by defining prototypes specific to object. The latter can be done by either Object.create() method or simply by declaring an object in normal JSON format. However, the former method provides much more control and the option of inheritence as well.

Prototypes of any "class" can be extended easily. However, extending native prototypes is not preferred unless we're shimming. Example Syntax : class_name.prototype.method_name = function (args) { /* Do something */}; Again, we can simply add this to the constructor if we have access to that. But this works too.

To extend an object, we can simply : object_name.method_name = function (args) { /* Do something */}; or through extension of inherited prototypes using Object.create().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment