/**
* OOP: ch.2 - Functions
* =====================================================
* Functions -
* Objects in JavaScript. Distinct because of an internal property
* named [[Call]]. The [[Call]] property is unique to functions
* and indicates that the object can be executed.
*
* Functions are first-class citizens.
* Use them like objects, assign them to variables,
* add them to objects, pass them to other functions as arguments,
* and return them from functions.
*
*/
//form: function declaration (hoisted in context)
function palindrome(str) {
var regEx = /([~!@#$%^&*()_+=`{}\[\]\|\\:;'<>,.-\/? ])+/g;
var tmpString = str.replace( regEx, '').toLowerCase();
var reverseTmpString = tmpString.split('').reverse().join('');
return (tmpString === reverseTmpString) ? true : false;
}
//form: function expression (lexical scope order)
var palindrome = function(str) {
var regEx = /([~!@#$%^&*()_+=`{}\[\]\|\\:;'<>,.-\/? ])+/g;
var tmpString = str.replace( regEx, '').toLowerCase();
var reverseTmpString = tmpString.split('').reverse().join('');
return (tmpString === reverseTmpString) ? true : false;
};
//first-class
// anotherPalindrome and palindrome
// reference same function
var anotherPalindrome = palindrome;
console.log("anotherPalindrome => ", anotherPalindrome('racecar'));
// function as param
var numbers = [ 1, 5, 8, 4, 7, 10, 2, 6 ];
numbers.sort(function(first, second) {
return first - second;
});
console.log(numbers); // "[1, 2, 4, 5, 6, 7, 8, 10]"
/*
// alphanumeric
objArray.sort(function(a, b) {
var textA = a.DepartmentName.toUpperCase();
var textB = b.DepartmentName.toUpperCase();
return (textA < textB) ? -1 : (textA > textB) ? 1 : 0;
});
*/
var t = setTimeout(function(){
// no comparison function
numbers.sort();
console.log(numbers); // "[1, 10, 2, 4, 5, 6, 7, 8]"
}, 1500);
/*
* Overloading:
* ============
* Ability of a single function to have multiple signatures
* is...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.