Assignment 1:3 Revised Definition of Closures

Introduction

This week’s assignment is designed to provide a definition of Closures to those students who enrolled in computer science courses with interests in Front-end development. The purpose is to give a sense of what closure is before step into front-end exploration and why it is necessary in the workplace.

Term: Closures (A term from CPEN400A class)

Parenthetical Definition:

The function implements a closure (a record storing function environment) inside itself.

Sentence Definition:

The closure is a programming technique implemented in programming languages (especially in JavaScript). This technique is usually implementing as a nested function storing enclosing function with its environment regardless of whether the enclosing function exists.

Expanded Definition:

The term Closure was defined by Peter J. Landin in 1964 when he was using a special machine for evaluating expressions. Nowadays, we use this term refers to those nested function which accesses the parent’s function to capture variables through its closure’ scope. Regardless of the old function returned or no longer exists, you can preserve all the elements of the state that the inner function refers to.

A simple closure example consists of a enclosing function return a child function. The basic framework displayed in the Code below. Inside the parent function, variables, object, other functions referred by closure could be stored in an unmanipulated place in order to protect the data from accidently change. The return function can be nested inside an Object because every function in JavaScript is Object. For example here below is a typical JavaScript closure.

function Counter(initial) {
	var val = initial;
	return {	
		increment : function() { val += 1; },
		reset: function() { val = initial; },
		get: function() { return val; }
	};
};

Analogy

A simple way to understand the closure is to compare it with scope. The scope is the limited lifetime for variables. For example, after you initialization of a local variable “iceCreamNum=10” in a function “howManyIceCream”, you own ten ice cream until the function expired. There is no beautiful memory of your ice cream been stored. At the moment, what you need is a closure put inside the function where your precious ice cream staying. In this way, the memory of holding ten ice cream will never fade away, you can still taste is when you get old.

visual

As shown in this picture, closure gives an ability to gain access to a variable declared in some other function scope.

Why Closures are useful?

The closure allows programs to remember the state in Web Applications especially when you have many different handlers construct parts of an object. In real front-end development, when AJAX working for transition, closures are very useful for callbacks in JavaScript by returning the callback function from the parent function. Besides, it is a way to emulate private variables in a complex working environment.

What do you need to be careful of?

Nested functions cannot access the parent function’s this and arguments because “this” is always a function local reference that is set by the caller of the function.

Leave a Reply

Your email address will not be published. Required fields are marked *

*