17.11.2015 Views

JavaScript_Succinctly

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Sample: sample79.html<br />

<br />

var yelp = function () {<br />

console.log('I am yelping!');<br />

// Functions return undefined even if we don't.<br />

}<br />

/* Logs true because a value is always returned, even if we don't<br />

specifically return one. */<br />

console.log(yelp() === undefined);<br />

<br />

The concept to take away here is that all functions return a value, even if you do not<br />

explicitly provide a value to return. If you do not specify a value to return, the value<br />

returned is undefined.<br />

Functions are first-class citizens (not just syntax, but values)<br />

In <strong>JavaScript</strong>, functions are objects. This means that a function can be stored in a<br />

variable, array, or object. Also, a function can be passed to and returned from a<br />

function. A function has properties because it is an object. All of these factors make<br />

functions first-class citizens in <strong>JavaScript</strong>.<br />

Sample: sample80.html<br />

<br />

// Functions can be stored in variables (funcA), arrays (funcB), and<br />

objects (funcC).<br />

var funcA = function () { }; // Called like so: funcA()<br />

var funcB = [function () { } ]; // Called like so: funcB[0]()<br />

var funcC = { method: function () { } }; // too.method() or<br />

funcC['method']()<br />

// Functions can be sent to and sent back from functions.<br />

var funcD = function (func) {<br />

return func<br />

};<br />

var runFuncPassedToFuncD = funcD(function () { console.log('Hi'); });<br />

runFuncPassedToFuncD();<br />

// Functions are objects, which means they can have properties.<br />

var funcE = function () { };<br />

92

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!