On Github quintons / jquery-talk-intermediate
Twitter: #quintons Github: https://github.com/quintons Stackoverflow: http://stackoverflow.com/users/684855/quinton 'The most misunderstood language has become the worlds most popular programming language'Douglas Crockford - Javascript architect, yahoo 'JavaScript like the browser is starting to move again'Alex Russell - Software engineer, Chrome team and (ECMA)TC39 representative
This will be my notesGood example
// single line
var result = (foo === true) ? console.log('true') : console.log('false');
// multiple lines
var result = (foo === true && bar === false) ?
console.log('true') : me === true ?
console.log('me true') : console.log('me false');
Bad example
// multiple lines
var result = (foo === true && bar === false) ?
console.log('true') : me === true ? console.log('me true') :
console.log('me false');
Good example
function Foo() {
return {
'bar': 'shot'
};
}
Bad Example
function Foo()
{
return
{
'bar': 'shot'
}
}
Good example
function Foo() {
return {
'bar': 'shot'
};
}
Bad Example
function Foo(){
return {
'bar': 'shot'
}
}
Good example
// code...
function namedFunction(){
return;
}
return {
'namedFunction': function(){
return;
}
}
Bad example
// code...
return {
'namedFunction': function namedFunction(){
return;
}
}
// Constructor always starts with a capital letter
function MyNewConstructor(){
this.foo = null;
}
// functions start with a small letter
function myNewFunction(){
return;
}
function expressions can only be used when applied to non-constructor functions Good example
function MyNewConstructor(){
this.foo = null;
}
Bad example
var MyNewConstructor = function(){
this.foo = null;
}
Good example
var myObjectLiteral = {
_privateFoo: function(){},
_privateBar: function(){}.
publicBar: function(){}
}
Bad example
var myObjectLiteral = {
privateFoo: function(){},
privateBar: function(){},
publicBar: function(){}
}
var module = (function(){
var _privateFunction = function(){
return;
}
var _privateMember = null;
return {
foo: function(){
return _privateFunction();
}
};
})()
module.foo();
// return users full name as string
function getFullName(i){
return names[i];
}
Do not waffle!
// this will return the users full name as a string
function getFullName(i){
return names[i];
}
Even better way
/**
* Return users full name as string
*
* @param {number} pass index of 'names' array
* @return {string} Return name string
*/
function getFullName(i) {
return names[i];
};
var TheFirstUserNameInThePastArrayFromJSON = getUserName(arr_userNames);
Camel Casing
what the...???!
var foo = document.getElementById('MyNewID'); // <-- camel 'Id'
var bar = foo.innerHTML(); //<-- capital 'HTML'
var xhr = new XMLHttpResponse(); //<-- camel 'Http'
Variables/functions examples
function Person(name){ //<-- object
this.name = name;
}
var myPerson = new Person('frank'); //<-- variable
Constants
var MAX_SPEED = 2000000;
var html = $('<div class="myClass">' + myContent + '</div>'); //<-- better to be in a template.
var html = $('<div style="width:' + myWidth + ';height:' + myHeight + ';">dollar sit</div>');
$('.myButton').on('click', function(event){
popup(event)
})
var popUp = function(event){
var that = $('.popup');
that.css({
left: event.clientX + 'px',
top: event.clientY + 'px'
})
that.show();
}
// better way...
$('.myButton').on('click', function(event){
popup(event.clientX, event.clientY)
})
var popUp = function(x, y){
var that = $('.popup');
that.css({
left: x + 'px',
top: y + 'px'
})
that.show();
}
Separate config data from application logic
var globalConfig = {
initialUrl: '/index',
companyName: 'foobar',
timeoutMs: '2000',
cache: true
}
var fundListConfig = {
maxItems: 100,
charLength: 200,
buyButton: false
}
This will be my notesClosure - basic example what is happening?
function addMe(a){
var n = (a*2) || 0;
return function(b){
return (n += (b || 0));
}
}
var myAdd1 = addMe();
myAdd1(5);
var res1 = myAdd1(5); // <-- what is res1?
var myAdd2 = addMe(5);
myAdd2();
myAdd2(5);
var res2 = myAdd2(5); // <-- what is res2?
Closure - example 1 what is happening? what is the error?
function buildList(list) {
var result = [];
for (var i = 0; i < list.length; i++) {
result.push( function() {alert('item' + list[i] + ' ' + list[i])} );
}
return result;
}
function testList() {
var fnlist = buildList([1,2,3]);
for (var j = 0; j < fnlist.length; j++) {
fnlist[j]();
}
}
buildList(['one','two','three','four']);
testList();
Closure - example 1 (fix)
function buildList(list) {
var result = [];
for (var i = 0; i < list.length; i++) {
(function(i){ // <-- an auto executing anynymous function creates scope!
result.push( function() {alert('item' + list[i] + ' ' + list[i])} );
})(i);
}
return result;
}
function testList() {
var fnlist = buildList([1,2,3]);
for (var j = 0; j < fnlist.length; j++) {
fnlist[j]();
}
}
buildList(['one','two','three','four']);
testList();
This will be my notesheight(), innerHeight(), innerWidth(), outerHeight(), outerWidth(), width()
<div class='someContent' style='height: 50px;width: 200px;padding: 10px;border: 5px;margin: 2px;'>
<!-- some content -->
</div>
console.log($('.someContent').height()); // returns '50'
height(), innerHeight(), innerWidth(), outerHeight(), outerWidth(), width()
<div class='someContent' style='height: 50px;width: 200px;padding: 10px;border: 5px;margin: 2px;'>
<!-- some content -->
</div>
console.log($('.someContent').innerHeight()); // returns '70'
height(), innerHeight(), innerWidth(), outerHeight(), outerWidth(), width()
<div class='someContent' style='height: 50px;width: 200px;padding: 10px;border: 5px;margin: 2px;'>
<!-- some content -->
</div>
console.log($('.someContent').innerWidth()); // returns '220'
height(), innerHeight(), innerWidth(), outerHeight(), outerWidth(), width()
<div class='someContent' style='height: 50px;width: 200px;padding: 10px;border: 5px;margin: 2px;'>
<!-- some content -->
</div>
console.log($('.someContent').outerHeight()); // returns '80'
console.log($('.someContent').outerHeight(true)); // returns '84' (inc margin)
height(), innerHeight(), innerWidth(), outerHeight(), outerWidth(), width()
<div class='someContent' style='height: 50px;width: 200px;padding: 10px;border: 5px;margin: 2px;'>
<!-- some content -->
</div>
console.log($('.someContent').outerWidth()); // returns '230'
console.log($('.someContent').outerWidth(true)); // returns '234' (inc margins)
height(), innerHeight(), innerWidth(), outerHeight(), outerWidth(), width()
<div class='someContent' style='height: 50px;width: 200px;padding: 10px;border: 5px;margin: 2px;'>
<!-- some content -->
</div>
console.log($('.someContent').width()); // returns '200'
This will be my notes