Show Menu
Cheatography

Javascript Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Closures

 

event delegation

 

this

 

prototypal inheri­tance

Math Class

Math.m­ax(­[va­lue1[, value2[, ...]]])

Functi­on.p­ro­totype

.apply()
The apply() method calls a function with a given this value, and arguments provided as an array (or an array-like object).
var max = Math.m­ax.a­pp­ly(­null, numbers);

Array.p­ro­totype.

arr.indexOf(search­Ele­ment[, fromIndex])
The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.
``
.find()
The find() method returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned.
var found = array1.fi­nd(­fun­cti­on(­ele­ment) {   return element > 10; });
arr.map()
The map() method creates a new array with the results of calling a provided function on every element in the calling array.
const map1 = array1.map(x => x * 2);

Objects

An object is a collection of proper­ties, and a property is an associ­ation between a name (or key) and a value. A property's value can be a function, in which case the property is known as a method. In addition to objects that are predefined in the browser, you can define your own objects.
A JavaScript object has properties associated with it. A property of an object can be explained as a variable that is attached to the object. Object properties are basically the same as ordinary JavaScript variables, except for the attachment to objects. The properties of an object define the charac­ter­istics of the object. You access the properties of an object with a simple dot-no­tation:
objectName.propertyName
var myCar = new Object(); myCar.make = 'Ford'; myCar.m­odel = 'Mustang'; myCar.year = 1969;
Unassigned properties of an object are undefined (and not null).
myCar.c­olor; // undefined
Properties of JavaScript objects can also be accessed or set using a bracket notation (for more details see property access­ors). Objects are sometimes called associ­ative arrays, since each property is associated with a string value that can be used to access it. So, for example, you could access the properties of the myCar object as follows:
myCar[­'make'] = 'Ford'; myCar[­'mo­del'] = 'Mustang'; myCar[­'year'] = 1969;
Property accessors provide access to an object's properties by using the dot notation or the bracket notatio
person = {'firs­tname': 'John', 'lastn­ame': 'Doe'}
person['firstname'] = 'Mario'; person­['l­ast­name'] = 'Rossi';
myOthe­rMe­tho­d(p­arams) { // ...do something else }
Object.Pr­ototype
Nearly all objects in JavaScript are instances of Object; a typical object inherits properties (including methods) from Object.pr­oto­type, although these properties may be shadowed (a.k.a. overri­dden). However, an Object may be delibe­rately created for which this is not true (e.g. by Object.cr­eat­e(n­ull)), or it may be altered so that this is no longer true (e.g. with Object.se­tPr­oto­typ­eOf).

Events

These are code structures which listen for things happening in browser, running code in response.
Attach event to an element
 
docume­nt.q­ue­ryS­ele­cto­r('­htm­l').on­click = function() {};
 
var myHTML = docume­nt.q­ue­ryS­ele­cto­r('­html'); myHTML.on­click = function() {};