Javascript: The Important Bits

28
JAVASCRIPT: THE IMPORTANT BITS REVIEWING JAVASCRIPT AND JQUERY FUNDAMENTALS AS WELL AS A BRIEF INTRO TO NODEJS Chris Saylor ( ) from Zumba Fitness ® @cjsaylor

description

Reviewing javascript and jQuery fundamentals as well as a brief introduction to nodejs.

Transcript of Javascript: The Important Bits

Page 1: Javascript: The Important Bits

JAVASCRIPT: THEIMPORTANT BITS

REVIEWING JAVASCRIPT AND JQUERY FUNDAMENTALS AS WELL AS A BRIEFINTRO TO NODEJS

Chris Saylor ( ) from Zumba Fitness ®@cjsaylor

Page 2: Javascript: The Important Bits

THINK YOU KNOW JAVASCRIPT?typeof [] === "array"; false0.1 + 0.2 === 0.3; falsea === null; false0 == '0'; true1 == '1'; true'' == '0'; false'' == 0; true'false' == false; false

True or false?

Page 3: Javascript: The Important Bits

THINK YOU KNOW JAVASCRIPT?

What does this return?

return{ a: "hello"}

Page 4: Javascript: The Important Bits

THINK YOU KNOW JAVASCRIPT?

Page 5: Javascript: The Important Bits

LET'S GET STARTED WITH THE BASICS.

Page 6: Javascript: The Important Bits

VARIABLESWhen declairing a variable without the "var", it puts the variable in

global space which can be problematic.

function hello() { test1 = 'hello'; // global scope var test2 = 'hello2'; // this function scope}

hello();

console.log(test1); // 'hello';console.log(test2); // undefined

Page 7: Javascript: The Important Bits

SCOPINGThere is no block scoping, only function scoping:

If you want to block the scope of the above loop:

for (var i = 0; i < 10; i++) { console.log(i);}console.log(i); // prints 10

(function () { for (var i = 0; i < 10; i++) { console.log(i); }}());var i;console.log(i); // undefined

Page 8: Javascript: The Important Bits

SCOPE IN CALLBACKSIn callbacks, you can share variables from the parent function:

var obj = { objValue: 'hello', test: function() { var self = this; setTimeout(function() { console.log(this.objValue); // undefined console.log(self.objValue); // 'hello' }, 10); }}

Page 9: Javascript: The Important Bits

OBJECTS AND "CLASSES"Objects are like JSON with some class aspects known as prototypes.

Page 10: Javascript: The Important Bits

OBJECTS AND "CLASSES"An example class:

Animal = (function() {

function Animal(name) { this.name = name; }

Animal.prototype.move = function() { return alert(this.name + ' moved.'); };

return Animal;

}());

Page 11: Javascript: The Important Bits

COMMON JAVASCRIPT PATTERNS

Page 12: Javascript: The Important Bits

IMMEDIATE EXECUTION FUNCTION

This immediately executes your logic as anonymous scope.

(function() { // Your logic here}());

Page 13: Javascript: The Important Bits

PRIVATE PATTERN

This pattern allows you to expose only what you want exposed.

var getCount = function() { var count = 0; return function() { return ++count; }}var next = getCount();console.log(next()); // 1console.log(next()); // 2

Page 14: Javascript: The Important Bits

INITIALIZATIONVariable initialization:

Complex object initialization:

var value = value || 'somevalue';

({ val1: 1, val2: null, init: function() { this.val2 = 2; return this; }}).init();

Page 15: Javascript: The Important Bits

LET'S GO OVER JQUERY OPTIMIZATION.

Page 16: Javascript: The Important Bits

SELECTOR CACHINGBad:

Good:

$('.someclass').text('replace some text.');$('.someclass').css('color', 'red');$('.someclass').focus();

$('.someclass') .text('replace some text.') .css('color', 'red') .focus();

Page 17: Javascript: The Important Bits

SELECTOR CACHINGCaching with callbacks.

Caching "this":

var $someotherclass = $('.someotherclass');$('.someclass').on('click', function(e) { $someotherclass.text('some event');});

$('.someclass').on('click', function(e)) { $this = $(this); $this.text('something'); $this.hide();});

Page 18: Javascript: The Important Bits

EVENT ATTACHINGWhen attaching events, use the "on" function.

What about dynamically generated links?

$('a').on('click', function(e)) { console.log('A link was clicked.');});

$(document).on('click', 'a', function(e)) { console.log('A link was clicked.');});

Page 19: Javascript: The Important Bits

PROPERLY STOPPING EVENTSReturning false is not always a good thing:

$('a').on('click', function(e) { console.log('Stopping propagation.'); return false; // Same as: // e.preventDefault(); // e.stopPropagation();});$('a').on('click', function(e)) { console.log('Another click.'); // Never gets called because of the // return false in the above event.});

Page 20: Javascript: The Important Bits

BASIC JQUERY PLUGIN STRUCTURE

Usage:

(function($) { $.fn.myLog = function() { return this.each(function() { console.log($(this)); }); }}(jQuery));

$('p').myLog();

Page 21: Javascript: The Important Bits

INTRODUCTION TO NODEJS

Page 22: Javascript: The Important Bits

Nodejs is an event-driven language built on Google's V8 (in c).

It's package manager is known as and is now packaged withnodejs.

npm

Page 23: Javascript: The Important Bits

NODEJS: HELLO WORLD

Source:

var http = require('http');http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n');}).listen(1337);console.log('Server running on port 1337');

http://nodejs.org/about/

Page 24: Javascript: The Important Bits

NODEJS: DEPENDANCY MANAGEMENTYou can manage dependencies for your nodejs app in package.json:

This allows us to install our project dependencies with npm:

{ "name": "sample-app", "version": "0.0.1", "dependencies": { "express": "2.5.x" }}

npm install

Page 25: Javascript: The Important Bits

NODEJS: EXPRESS SERVEROur hello world example in express:

var express = require('express');app = express.createServer()app.get('/', function(req, res) { res.send('Hello World');});app.listen(1337);console.log('Listening on port 1337');

Page 26: Javascript: The Important Bits

NODEJS: CONNECT MIDDLEWARERouting middleware is anything that implements the request,

response, and next function pattern.

Using this middleware:

// Request loggerfunction logger(req, res, next) { console.log("Path requested: " + req.path); next();}

app.get('/', logger, function(req, res) { res.send('Hello World');});

Page 27: Javascript: The Important Bits

QUESTIONS?

Page 28: Javascript: The Important Bits