The important parts of the front end development sphere including CSS3, advanced JavaScript, libraries such as jQuery, RequireJS and Promises. And finally, chrome developer tools for successful debugging and editing.
5. LET'S FIND OUT
1. Is it possible to do shadows with CSS?
2. Who can write such function?
var result = addNumbers(2)(3);
6. LET'S FIND OUT
1. Is it possible to do shadows with CSS?
2. Who can write such function?
var result = addNumbers(2)(3);
3. Who can define "this"?
7. LET'S FIND OUT
1. Is it possible to do shadows with CSS?
2. Who can write such function?
var result = addNumbers(2)(3);
3. Who can define "this"?
4. How to call a function after completion of
two async functions together?
8. OUTLINE
I. CSS
1. Basics
2. CSS3
3. LESS
II. JavaScript
1. Functions: objects
2. Lexical scope: closures, module pattern
3. Execution contexts and "this" value
III. Tools and Libraries
1. RequireJS
2. jQuery
3. Promises
4. Chrome Dev Tools
10. CSS :: SELECTORS
Selector Description Example
*
Matches any element.
E Matches any E element (i.e., an
element of type E).
a
E F Matches any F element that is a
descendant of an E element.
div a
E > F Matches any F element that is a
child of an element E.
div > a
E:first-child Matches element E when E is the
first child of its parent.
li:first-child
E:link
E:visited
Matches element E if E is the source
anchor of a hyperlink of which the
target is not yet visited (:link) or
already visited (:visited).
a:link
a:visited
11. CSS :: SELECTORS (cont.)
Selector Description Example
E:active
E:hover
E:focus
Matches E during certain user
actions.
a:active
a:hover
a:focus
E + F Matches any F element immediately
preceded by a sibling element E.
div + div
E[foo] Matches any E element with the
"foo" attribute set (whatever the
value).
div[data-id]
E[foo="warning"] Matches any E element whose "foo"
attribute value is exactly equal to
"warning".
input[type=”text”]
DIV.warning Language specific. (In HTML, the
same as DIV[class~="warning"].)
div.navigation
E#myid Matches any E element with ID equal
to "myid".
div#main
27. FUNCTIONS :: LEXICAL ENV :: def
Definition:
A Lexical Environment is a specification type used to define
the association of Identifiers to specific variables and
functions based upon the lexical nesting structure of
ECMAScript code.
33. PROBLEM
<div>
I am div 1
</div>
<div>
I am div 2
</div>
<div>
I am div 3
</div>
var i, divs, len;
i = 0;
divs=document.getElementsByTagName('div');
len = divs.length;
for (i; i < len; i += 1) {
divs[i].addEventListener('click',
function () {
alert('I am div ' + i);
}, false)
}
http://jsbin.com/acoceb/1/edit
35. PROBLEM
<div>
I am div 1
</div>
<div>
I am div 2
</div>
<div>
I am div 3
</div>
var i, divs, len;
i = 0;
divs = document.getElementsByTagName
('div');
len = divs.length;
for (i; i < len; i += 1) {
(function (id) {
divs[i].addEventListener('click',
function () {
alert("I am div " + (id + 1));
}, false);
})(i);
}
http://jsbin.com/acoceb/5/edit
39. FUNCTIONS :: EXECUTION CONTEXT
Every line of JavaScript code is run in an
“execution context.”
ExecCont = {
LexicalEnv: {},
VarEnv: {},
ThisBinding: {}
}
40. FUNCTIONS :: EXECUTABLE CODE TYPES
● Global code
alert("hello world");
● Function code
function callMe () {
alert('hello world');
}
● Eval code
41. FUNCTIONS :: GLOBAL CONTEXT
var a = 1,
b = 3
c = a + b;
alert(c); // 4
alert(this); // window
42. FUNCTIONS :: FUNC CONTEXT :: method
var a = {
b: function() {
return this;
}
};
a.b(); //a;
a['b'](); //a;
var c = {};
c.d = a.b;
c.d(); //c
43. FUNCTIONS :: FUNC CONTEXT :: function
function a () {
return this;
}
var A = a(); // window
44. FUNCTIONS :: FUNC CONTEXT :: function
var a = {
b: function() {
return this;
}
};
var foo = a.b;
foo(); //window
45. FUNCTIONS :: FUNC CONTEXT :: function
var a = {
b: function() {
var c = function() {
return this;
};
return c();
}
};
a.b(); //window
48. function Builder () {
this.name = 'Arnold';
}
var b = new Builder();
// b = {
// name: 'Arnold'
// }
FUNCTIONS :: FUNC CONTEXT :: new
49. FUNCTIONS :: FUNC CONTEXT :: reference
Execution Context Syntax of function call Value of this
Global n/a global object (e.g. window)
Function
Method call:
myObject.foo();
myObject
Function
Baseless function call:
foo();
global object (e.g. window)
(undefined in strict mode)
Function
Using call:
foo.call(context, myArg);
context
Function
Using apply:
foo.apply(context, [myArgs]);
context
Function
Constructor with new:
var newFoo = new Foo();
the new instance
(e.g. newFoo)
Evaluation n/a value of this in parent context
source: http://javascriptweblog.wordpress.com/2010/08/30/understanding-javascripts-this/
52. REQUIREJS :: WHY?
● Web sites are turning into Web apps
● Code complexity grows as the site gets
bigger
● Assembly gets harder
● Developer wants discrete JS files/modules
● JavaScript doesn't have import yet
source: http://requirejs.org/docs/why.html
64. JQUERY :: SELECTORS
$(selector).method()
For example, $(‘#list’) will return the elements which has the attribute id=”list”.
For more, see http://api.jquery.com/category/selectors/.
73. TOOLS :: PROMISES :: def
In computer science, future, promise, and delay refer to
constructs used for synchronizing in some concurrent
programming languages. They describe an object that acts as a
proxy for a result that is initially unknown, usually because the
computation of its value is yet incomplete.
The term promise was proposed in 1976 by Daniel P. Friedman
and David Wise,[1] [...]
The terms future, promise, and delay are often used
interchangeably, although some differences in usage between
future and promise are treated below. Setting the value of a
future is also called resolving, fulfilling, or binding it.
Source: http://en.wikipedia.org/wiki/Futures_and_promises
75. TOOLS :: PROMISES :: Deferred Object
jQuery.Deferred() A constructor that creates a new Deferred object, can take an optional
initFunction parameter that will be called right after the deferred has
been constructed.
jQuery.when() A way to execute callback functions based on one or more objects that
represent asynchronous tasks.
jQuery.ajax() Performs an asynchronous Ajax requests where the jqXHR objects
returned by $.ajax() implement the Promise interface, giving them all
the properties, methods, and behaviour of a Promise.
deferred.resolve(arg1, arg2, ...) Resolve a Deferred object and call any 'done' Callback with the given
arguments.
deferred.reject(arg1, arg2, ...) Reject a Deferred object and call any 'fail' Callback with the given
arguments.
deferred.promise() Returns a promise, that is an immutable view of the deferred object: the
promise can be passed around safely since the underlying deferred
cannot be resolved or rejected through it.
76. TOOLS :: PROMISES :: Promise Object
deferred.then(resolveCallback,
rejectCallback)
Handlers to be called when the Deferred object is resolved or
rejected.
deferred.done() Functions or array of functions that are called when the Deferred
object is resolved.
deferred.fail() Functions or array of functions that are called when the Deferred is
rejected.
deferred.isResolved Determine whether a Deferred object has been resolved.
77. TOOLS :: PROMISES :: side 1
$.when(
// call functions
startChannel(),
drawLayout()
).done(function () {
// do when both are done
initModule.start();
})
78. TOOLS :: PROMISES :: side 2
function drawLayout () {
var dfd = $.Deferred();
function loadSettings() {
// do some ajax request;
}
function validate() {
...
// in case validation failed
dfd.reject();
}
function render() {
// render
return dfd.resolve();
}
return dfd.promise();
}