JSFiddle - React, Tailwind, and code Playground

OOP-: Ch2- Functions

by nickadeemus2002

JavaScript

/**
 * 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...