Git Product home page Git Product logo

cs3216-js-workshop's Introduction

JavaScript

The Language of the Web

What this Workshop is About

  • JavaScript and what it can do
  • Kickstart your learning in JavaScript
  • NOT a crash course on JavaScript

Introduction

  • Also known as Mocha, LiveScript, JScript, ECMAScript
  • Developed in an extremely short amount of time
  • Scripting language
    • Not compiled, interpreted and executed on-the-fly
  • Dynamic typing
  • Supports object-oriented, imperative and functional programming styles
  • Despite its name, it has nothing to do with Java

What can JavaScript do?

  • Provides interactivity to web applications
  • Mainly executed in the browser to:
    • Manipulate DOM elements
    • Load content into the document without reloading
  • Can be used in an application's back end using NodeJS

Getting Started

  • No installation needed!
    • Unless you're doing Node
  • JavaScript comes with all browsers. Yes, even IE!
  • Simply fire up your browser's console

Language and Syntax

  • Similar to C
  • Learn it yourself (:
  • But beware of wats... (from 1:22 onwards)
  • Read more about JS quirks here

Points to Note

  • Case-sensitive
    • getElementById !== getElementByid
  • Semicolons are optional
    • But please add them
  • Variable scoping
    • Blocks do not have scope
    • Only functions do
    • Global variables

Use Your Semicolons

  • Automatic Semicolon Insertion (ASI)
// Before ASI
a = b + c
foo()

// After ASI
a = b + c; foo() // All is good
  • However, ASI is only applied if the parser needs to do so in order to make sense of the code in question.
// Before ASI
a = b + c
[1].push(a)

// After ASI
a = b + c[1].push(a) // KABOOM!

Use Semicolons

Variable Scoping

  • Declaring variables without the var keyword:
> var foo = function () { bar = 1; }
> foo();
> console.log(bar); // bar is now a global variable
1
  • You almost never want to use globals

Closures

  • Closures are functions that refer to independent variables.
  • The function defined in the closure 'remembers' the environment in which it was created.
function makeFunc () {
  function displayName() {
    alert(name);
  }
  var name = 'CS3216 Rocks!';
  return displayName;
}

var myFunc = makeFunc();
myFunc();
  • A function factory can create closures with different environments
function makeAdder (x) {
  return function (y) {
    return x + y;
  };
}

var add5 = makeAdder(5);
var add10 = makeAdder(10);

console.log(add5(2));  // 7
console.log(add10(2)); // 12

Callbacks

  • Callback functions are derived from functional programming
  • A function is passed into another function as a parameter
  • Callback functions are used in:
    • Asynchronous executions
    • In Event Listeners/Handlers
    • In setTimeout and setInterval methods
  • Read more on JavaScript callbacks here
  • Callback Joke
  • Common beginner mistakes
for (var i = 0; i < 5; i++) {
  setTimeout(function () {
    console.log(i);
  }, i * 1000);
}
// vs
for (var i = 0; i < 5; i++) {
  function print (x) {
    setTimeout(function () {
      console.log(x);
    }, x * 1000);
  }
  print(i);
}

JS in the Browser

  • To run javascript in your HTML file, simply do:
<script src="myscript.js"></script>
// or
<script> console.log('Hello World!'); </script>

Manipulating DOM

  • Retrieving DOM elements using JavaScript:
document.getElementById('some-id');
document.getElementsByClassName('some-class');
document.getElementsByTagName('some-tag');
  • Try this on IVLE class roster!
var rows = document.querySelectorAll('tr[class*="dataGridCtrl-Alter"], tr[class*="dataGridCtrl-Item"]');
for (var i = 0; i < rows.length; i++) {
  rows[i].childNodes[3].innerHTML = 'Nala Cat';
  var img = rows[i].querySelectorAll('img')[0];
  img.src = 'http://nalacat.com/wp-content/uploads/2013/01/photo-2.jpg';
}

Event Binding

  • Attach functions to events
    • Examples of events: click, focus, blur, hover, change, keydown, etc
<div class="clickable" onclick="handleClick();"></div>
<div class="focusable" onfocus="handleFocus();"></div>
<div class="keyable" onkeyup="handleKeyup();"></div>

function handleClick() { ... }
function handleFocus() { ... }
function handleKeyup() { ... }

Vanilla JS

  • Vanilla JS is a fast, lightweight, cross-platform framework for building incredible, powerful JavaScript applications
  • Used by Facebook, Google, Twitter, YouTube, Yahoo, Wikipedia, etc
  • In fact, Vanilla JS is already used on more websites than jQuery, Prototype JS, MooTools, YUI, and Google Web Toolkit - combined
  • Download the source here

jQuery

  • jQuery is a cross-platform JavaScript library designed to simplify the client-side scripting of HTML
  • Write less, do more
  • Use either the jQuery or $ object
  • Get the source here

Basics

$(document).ready(function () {
  // Do your stuff 
});
  • Have to wait for DOM to load before you start manipulating it

Getting DOM

Vanilla jQuery
document.getElementById('some-id') $('#some-id')
document.getElementByClassName('some-class') $('.some-class')
document.getElementByTagName('some-tag') $('some-tag')

Event Handling

Vanilla jQuery
<div class="clickable" onclick="handleClick();"></div> $('div').click(function () { ... })
<div class="focusable" onfocus="handleFocus();"></div> $('div').focus(function () { ... })
<div class="keyable" onkeyup="handleKeyup();"></div> $('div').on('keyup', function () { ... })

Animations

  • jQuery comes with some handy animations including: fadeIn, fadeOut, hide, slideUp
  • jQUery animations are slow
  • Use CSS3 animations instead

DOM Injection

  • Beware of DOM injection when rendering user-submitted content on your webpage!
  • Use jQuery's .text() method for encoding of special characters such as < and >

JavaScript Tools

  • UnderscoreJS
    • A library of functional programming helpers, such as map, filter, reduce, etc
    • Recommended for functional all programmers
  • RequireJS
    • Forces you to write modular javascript
    • Handles nested dependencies
  • Bower
    • A package manager, not a library
    • Keeps your libraries structured

JavaScript Frameworks

JavaScript Resources

Readings

References

cs3216-js-workshop's People

Contributors

yangshun avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.