Skip to content

Instantly share code, notes, and snippets.

@ryankinal
Last active October 27, 2016 16:40
Show Gist options
  • Save ryankinal/ea5bd76816f81cc22419 to your computer and use it in GitHub Desktop.
Save ryankinal/ea5bd76816f81cc22419 to your computer and use it in GitHub Desktop.
JS Curriculum

JS Curriculum

Goals:

  • Know the language.
  • HTTP requests/responses
  • Be able to create client-side and server-side code effectively, with a focus toward the toolsets being used for McKissock S1.
  • A probject of some hopefully useful sort

Sections

HTML

  1. HTML
  2. Scripts
  3. Styles

Language

  1. Data types, objects, and arrays
  2. Control structures
  3. Functions and scope
  4. Inheritance

Client

  1. Chrome
  2. console
  3. debugger
  4. inspector
  5. Modules and "namespacing"
  6. DOM
  7. Accessing nodes
  8. Adding nodes
  9. Removing nodes
  10. Event listeners
  11. Storage (cookies and localStorage)
  12. XHR
  13. Angular
  14. Apps
  15. Controllers
  16. Directives

HTTP

  1. Request and response
  2. Methods
  3. Headers
  4. Body
  5. Relationship to REST and APIs

Server

  1. node and npm
  2. modules
  3. file access
  4. database access
  5. http and routing
  6. express
  7. sequelize
  8. yo angular-fullstack

Resources

Data Types, Objects, and Arrays

Note: The unary operator typeof returns the type of the value stored in its operand. typeof x will return the type of value stored in the variable x

Data Types / Values

number

This is the type of all numbers - integers, decimals, and "special numbers" - Infinity, -Infinity and NaN (Not a Number). Yeah, strange.

string

Single quotes or double quotes will work as long as they match. I tend to use single quotes, and that seems to be the style enforced by helium and related projects.

  • "this is a string"
  • 'this is also a string'

boolean

true or false - easy enough.

Other values can be treated as "truthy" or "falsy", meaning that when used in a conditional, they will essentially evaluate to true or false. See the MDN articles below for a good explanation of truthy and falsy values.

undefined

undefined is both a value and a type of value. typeof undefined will return "undefined". When a variable x has not been assigned a value, its value is undefined and typeof x will return "undefined".

Objects

Objects are awesome! Everything is an object in JS, or it is converted ("coerced") to one when necessary. Objects are used all the time in JS, for configuration, arguments to functions, returns from functions, and so many more awesome things.

Creating an object

var obj = {
  x: 24,
  y: 36
  f: function() {
    return x + y;
  },
  a: []
};

As demonstrated, an object can contain any kind of value. It is essentially an untyped name/value store. This can then be passed around to functions and other things. Unlike primitive values, Objects are passed by reference, so when you pass an object to a function, you're dealing with the same object. Note that this is something that javascript controls and is not a choice of the programmer. This applies to all objects - so if a number has been coerced to a Number (an object wrapper for the number), then your number will pass by reference. Also note that Arrays are objects, so they will be passed by reference.

var obj = { ... };
var f = function(o) {
  return o === obj;
};
console.log(obj); // true

Functions

Functions are objects. They are also first-class values, which means they can be stored in variables and passed as arguments to functions. This is a powerful and popular feature of JavaScript. There are a couple ways to declare functions.

function f(x) {
	return x * 2;
}

var g = function(x, y) {
	return x(y) * 3;
};

Functions f and g can then be passed to other functions. For instance, I could call g(f, 2), which would return 12 (The argument 2 times 2 in function f then the result multiplied by 3). The array functions listed below take functions as their arguments, and could easily take the function f instead of the functions I've given them.

Arrays

Arrays, like objects, are untyped. They are 0-indexed. There are several ways to create them, but by far the most common is the array literal.

var arr = [];
var arr2 = [1, 2, {}, [], Infinity];

As demonstrated, anything can go into an array. Arrays are full of awesome functionality, some of which is listed below, and the rest of which can be found in the MDN Array documentation

Changing arrays

There are four basic functions for adding elements to an array (and a couple more advance ones that I'll skip for now). These functions change the array in place; in these examples arr will be changed.

  • arr.push(6) - Adds 6 to the end of the array
  • arr.pop() - Removes and returns the last element in the array
  • arr.unshift(6) - Adds 6 to the beginning of the array
  • arr.shift() - Removes and returns the first element in the array

forEach

Arrays have a function called forEach, which takes a function as an argument, and iterates over the array. This is the preferred method of iteration.

[1, 2, 3, 4, 5].forEach(function(item) {
	console.log(item);
});

This will output each element in the array to the console

map

Arrays have a function called map, which takes a function as an argument, and iterates over the array, applying the function to each element in the array, and returning a new array with the result.

var arr = [1, 2, 3, 4, 5].map(function(item) {
	return item * 2;
});

This returns a new array, leaving the original alone. The new array will look like this: [2, 4, 6, 8, 10].

filter

Arrays have a function called filter, which takes a functional as an argument and iterates over the array, applying the function to each element in the array. If the function returns a truthy value, the element will be in the resulting array; if the function returns a falsy value, the element will not be in the resulting array.

var arr = [1, 2, 3, 4, 5].filter(function(item) {
	return item % 2 === 0; // divisible by 2
});

This returns a new array, leaving the original alone. The new array will look like this: [2, 4]

A note on compatibility

These array functions are only in newer JavaScript engines. Modern browsers implement them, but older ones may not. In order to get this functionality, libraries like lodash and underscore provide similar functions.

HTML
We will probablly go over various aspects of HTML and CSS as necessary, but here is a basic HTML document
<!DOCTYPE html>
<html lang="en">
<head>
<title>This goes in the browser tab</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container">
<div class="header">
<h1>This is a test</h1>
<p>Just demonstrating some stuff with HTML and CSS</p>
</div>
<div class="columns">
<div class="column">
<h2>HTML</p>
<p>Hypertext Markup Language</p>
<ul>
<li>Document structure</li>
<li>The main entry point for everything</p>
<li>Static, on it's own</li>
</ul>
</div>
<div class="column">
<h2>CSS</h2>
<p>Cascading StyleSheets</p>
<ul>
<li>Document appearance</li>
<li>Defines layout</li>
<li>Makes things pretty</li>
</ul>
</div>
<div class="column">
<h2>JS</h2>
<p>JavaScript</p>
<ul>
<li>Document functionality</li>
<li>Defines &quot;extra&quot; things that happen</li>
<li>Makes things work</li>
</ul>
</div>
</div>
<div class="footer">
<p>&copy; Ryan Kinal DON'T TOUCH</p>
</div>
</div>
<script type="text/javscript" src="script.js"></script>
</body>
</html>
CSS
* {
box-sizing: border-box;
}
body {
margin: 0;
padding: 0;
}
.container {
width: 80%;
margin: 0 10%;
background-color: #EEE;
}
.header,
.columns,
.footer {
clear: both;
}
.header {
text-align: center;
background-color: #333;
color: #FFFFFF;
}
.columns {
overflow: hidden;
}
.column {
width: 33%;
padding: 15px;
}
.column h2 {
text-align: center;
}
.footer {
text-align: center;
}
JS
(function() {
var now = new Date(),
year = now.getYear() + 1900,
copyrightDateElem = document.querySelector('#copyDate'),
newText = document.createTextNode(year);
copyrightDateElem.appendChild(newText);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment