My JavaScript Cheat Sheet!

Week 4 of HTML, CSS, and Javascript for Web Developers!

Notes I have taken:
// Variable Declaration
var x;
 
// Function Declaration
function a() {
    // Do something!!
}
 
var b = function() {
    // Do something!!
}
 
// Function Invocation
a();
b();
 
// Function Declaration with Arguments and a Return Value..
function c(x, y) {
    return x > y;
}
 
// Invoke with parameters, all legal:
c(9,5); // true
c(9,"a"); // false
c(9,'5'); // true
c('a','z'); // false
c('z','a'); // true
c(); // false
c(90); // false
 
// Global Scope Example:
var globalMessage = "This is the global message!";
 
// Function Scope Example
function d() {
    globalMessage = "Global message overriden!";
    return globalMessage;
}
 
 
// What is false in JavaScript?
if (false || null || undefined || "" || 0 || NaN) {
    // All above is false!
}
 
if (true && "hello" && 1 && -1 && "false") {
    // All above is true!
}
 
// Object Creation
var sampleObject = new Object();
sampleObject.someProperty = "This property is created!";
sampleObject.someOtherProperty = new Object();
sampleObject.someOtherProperty.andAnotherOne = "Blue";
sampleObject["stupid property name"] = "Stupid Property Value";
 
// Object Creation, another way..
var koraytugay = {
    firstname: "koray",
    lastname: "tugay",
    preferences: {
        drink: "milk",
        eat: "bread"
    },
    "a long property name": "and the value for it"
};
 
// Loop over properties of an object:
for (var prop in koraytugay) {
}
 
// Functions are Objects in JavaScript..
function multiply(x, y) {
    return x * y;
}
multiply.functionProperty = "This is valid in JavaScript!";
 
// Creating a function within a function and returning it as a value!
function makeMultiplier(multiplier) {
    return function (x) {
        return multiplier * x;
    }
}
 
var by10multiplier = makeMultiplier(10);
var res = by10multiplier(20); // 200
 
// Passing functions as arguments!
function doOperation(x, operation) {
    return operation(x);
}
var foo = doOperation(5, by10multiplier); // 50
 
// Function Constructors
// A Function Constructor can not return anything!
function Circle(radius) {
    this.radius = radius;
}
 
// Not for every instance of object, but rather for the 'class'!
Circle.prototype.getArea = function () {
    return Math.PI * Math.pow(this.radius, 2);
}
 
var myCircle = new Circle(1); // Circle {radius: 1}
var myCircleArea = myCircle.getArea(); // 3.14...
 
var mySquare = {
    sideLength: 5,
    getSquareArea: function () {
        return this.sideLength * this.sideLength;
    },
};
 
mySquare.getSquareArea(); // 25
 
// Arrays
var myArray = new Array();
myArray[0] = "Koray Tugay";
myArray[1] = 1984;
myArray[2] = function (name) {
    return "Your name is: " + name;
}
 
myArray[2](myArray[0]); // Your name is: Koray Tugay
 
// Shorthand array creation:
var myOtherArray = ["Koray Tugay", 1984, myArray[2]];
 
// Add an Object to the array:
myOtherArray[3] = {
    name: "Somevalue here!"
};
 
// Loop over the array:
for (var i = 0; i < myOtherArray.length; i++) {
    // do something..
}
 
// This works as well:
for (index in myOtherArray) {
    myOtherArray[index];
}
 
// (Fake) Namespaces!
var myFakeNS = {};
myFakeNS.name = "Koray Tugay";
myFakeNS.greet = function(name) {
    return "Hello " + name;
}
 
var greetKorayTugay = myFakeNS.greet(myFakeNS.name); // Hello Koray Tugay
 
// Immediatly Invoked Functon Expressions!
// We first create a Function Object, and immediatly invoke it by () after it!
(function () {
    // This function is invoked immediatly!!!
})();
 
var myNickName = "Danger";
// We can even pass parameters to it!
(function (text) {
    // Hello + text!
})(myNickName);
 
// We can encapsulate and expose our namespaces this way:
(function(window) {
    var myOtherFakeNS = {};
    // Some functions here for myOtherFakeNS!
    window.myOtherFakeNS = myOtherFakeNS;
}) (window)

And my solution to the Week 4 Assignment..

SpeakHello.js
(function(window){
    var helloSpeaker = {};
 
    var speakWord = "Hello";
    helloSpeaker.speak =  function(name) {
        console.log(speakWord + " " + name);
    };
 
    window.helloSpeaker = helloSpeaker;
})(window);

SpeakGoodBye.js
(function(window){
    var byeSpeaker = {};
 
    var speakWord = "Good Bye";
    byeSpeaker.speak =  function(name) {
        console.log(speakWord + " " + name);
    };
 
    window.byeSpeaker = byeSpeaker;
})(window);

script.js
(function(){
    var names = ["Yaakov", "John", "Jen", "Jason", "Paul", 
        "Frank", "Larry", "Paula", "Laura", "Jim"];
    for (var name in names) {
        var firstChar = names[name].charAt(0).toLowerCase();
        if (firstChar === 'j') {
            byeSpeaker.speak(names[name]);
        } else {
            helloSpeaker.speak(names[name]);
    }
    }
})();