Javascript: The Important Bits
-
Upload
zumba-fitness-technology-team -
Category
Technology
-
view
1.265 -
download
0
description
Transcript of Javascript: The Important Bits
![Page 1: Javascript: The Important Bits](https://reader034.fdocuments.us/reader034/viewer/2022042813/547cfce1b4795993508b47b0/html5/thumbnails/1.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042813/547cfce1b4795993508b47b0/html5/thumbnails/2.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042813/547cfce1b4795993508b47b0/html5/thumbnails/3.jpg)
THINK YOU KNOW JAVASCRIPT?
What does this return?
return{ a: "hello"}
![Page 4: Javascript: The Important Bits](https://reader034.fdocuments.us/reader034/viewer/2022042813/547cfce1b4795993508b47b0/html5/thumbnails/4.jpg)
THINK YOU KNOW JAVASCRIPT?
![Page 5: Javascript: The Important Bits](https://reader034.fdocuments.us/reader034/viewer/2022042813/547cfce1b4795993508b47b0/html5/thumbnails/5.jpg)
LET'S GET STARTED WITH THE BASICS.
![Page 6: Javascript: The Important Bits](https://reader034.fdocuments.us/reader034/viewer/2022042813/547cfce1b4795993508b47b0/html5/thumbnails/6.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042813/547cfce1b4795993508b47b0/html5/thumbnails/7.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042813/547cfce1b4795993508b47b0/html5/thumbnails/8.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042813/547cfce1b4795993508b47b0/html5/thumbnails/9.jpg)
OBJECTS AND "CLASSES"Objects are like JSON with some class aspects known as prototypes.
![Page 10: Javascript: The Important Bits](https://reader034.fdocuments.us/reader034/viewer/2022042813/547cfce1b4795993508b47b0/html5/thumbnails/10.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042813/547cfce1b4795993508b47b0/html5/thumbnails/11.jpg)
COMMON JAVASCRIPT PATTERNS
![Page 12: Javascript: The Important Bits](https://reader034.fdocuments.us/reader034/viewer/2022042813/547cfce1b4795993508b47b0/html5/thumbnails/12.jpg)
IMMEDIATE EXECUTION FUNCTION
This immediately executes your logic as anonymous scope.
(function() { // Your logic here}());
![Page 13: Javascript: The Important Bits](https://reader034.fdocuments.us/reader034/viewer/2022042813/547cfce1b4795993508b47b0/html5/thumbnails/13.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042813/547cfce1b4795993508b47b0/html5/thumbnails/14.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042813/547cfce1b4795993508b47b0/html5/thumbnails/15.jpg)
LET'S GO OVER JQUERY OPTIMIZATION.
![Page 16: Javascript: The Important Bits](https://reader034.fdocuments.us/reader034/viewer/2022042813/547cfce1b4795993508b47b0/html5/thumbnails/16.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042813/547cfce1b4795993508b47b0/html5/thumbnails/17.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042813/547cfce1b4795993508b47b0/html5/thumbnails/18.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042813/547cfce1b4795993508b47b0/html5/thumbnails/19.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042813/547cfce1b4795993508b47b0/html5/thumbnails/20.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042813/547cfce1b4795993508b47b0/html5/thumbnails/21.jpg)
INTRODUCTION TO NODEJS
![Page 22: Javascript: The Important Bits](https://reader034.fdocuments.us/reader034/viewer/2022042813/547cfce1b4795993508b47b0/html5/thumbnails/22.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042813/547cfce1b4795993508b47b0/html5/thumbnails/23.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042813/547cfce1b4795993508b47b0/html5/thumbnails/24.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042813/547cfce1b4795993508b47b0/html5/thumbnails/25.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042813/547cfce1b4795993508b47b0/html5/thumbnails/26.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042813/547cfce1b4795993508b47b0/html5/thumbnails/27.jpg)
QUESTIONS?
![Page 28: Javascript: The Important Bits](https://reader034.fdocuments.us/reader034/viewer/2022042813/547cfce1b4795993508b47b0/html5/thumbnails/28.jpg)