A JS workshop for charity. Entry fee - a piece of warm clothing for the less privileged in Bangladesh.
I speak about Objects and Functions in JS and NodeJS with hands-on coding sessions.
4. Objects
Coding for Kombol | Riyadh Al Nur | Objects and Functions
● Standalone entity with a name and
property
● Object and property names are both
case-sensitive
● Everything’s an object except for null
and undefined
var myObj = {};
var myObj = new Object();
myObj.property1
myObj.Property1 // not the same as above
5. Object Properties
Coding for Kombol | Riyadh Al Nur | Objects and Functions
● Object properties can be of any type -
strings, functions, arrays
● Properties can be accessed by both dot
and bracket notation
var myObj.prop1 = ‘Hello’;
var myObj[‘prop1’] = ‘Hello’;
6. Instantiation
Coding for Kombol | Riyadh Al Nur | Objects and Functions
● Object initialisers
● Using constructor functions
● Object.create() method
var myObj = {};
function foo() {};
var bar = new foo();
var foo = {};
var bar = Object.create(foo);
7. Inheritance
Coding for Kombol | Riyadh Al Nur | Objects and Functions
● JavaScript is a prototype-based language
● All objects inherit from some other
object
● All properties available in prototype
object of constructor
var foo = {};
foo.prototype.prop1 = function() {
return ‘Hello World!’;
};
var bar = new foo();
bar.prop1();
// Hello World!
8. Methods vs. Prototypes
Coding for Kombol | Riyadh Al Nur | Objects and Functions
● Prototype based instances creates single
instance of properties
● Methods create instance for every single
invocation
● Properties attached via prototype are
shared
● The opposite for method based
properties
9. Functions
Coding for Kombol | Riyadh Al Nur | Objects and Functions
● First class members in JS
● To be exact, they are first-class objects
● Not the same as procedures
function add(a, b) {
return a + b;
}
// add(2,3) -> 5
10. Function Scope
Coding for Kombol | Riyadh Al Nur | Objects and Functions
● Functions can access variables inside the
scope it’s defined in only
● Functions inside another function can
access parent variables
var a = 2, b = 3;
function add() {
return a + b; // can access a & b
}
// add() -> 5
11. Recursion
Coding for Kombol | Riyadh Al Nur | Objects and Functions
● Functions can refer and call themselves
● 3 ways to do so:
● function name
● arguments.callee()
● in-scope variable that refers to
function
var foo = function bar() {};
// 1. bar()
// 2. arguments.callee()
// 3. foo()
12. Closures
Coding for Kombol | Riyadh Al Nur | Objects and Functions
● Inner function has access to outer
function’s variables
● Outer function does not have access to
inner function’s variables
var foo = (function bar() {
var string = ‘Hello’;
function strings() {
return string + ‘ World!’;
}
return strings();
})();
foo(); // Hello World!
13. Closures
Coding for Kombol | Riyadh Al Nur | Objects and Functions
● But, what happens when we do
something like this?
var myName = function (name) {
return {
setMyName: function(name) {
name = name;
}
};
};
14. Closures
Coding for Kombol | Riyadh Al Nur | Objects and Functions
● this refers to where a function is called,
not where it was defined!
16. The fact
Coding for Kombol | Riyadh Al Nur | Objects and Functions
● NodeJS is a JavaScript runtime
environment - not a language!
17. So why is it so special?
Coding for Kombol | Riyadh Al Nur | Objects and Functions
● Event driven
● Non-blocking I/O API
● Real-time apps someone?
Not so special but crucial points
● Runs on Google’s V8 JS engine
● Now front-end and back-end are in the
same language!
18. Under the hood
Coding for Kombol | Riyadh Al Nur | Objects and Functions
● Runs on a single-thread
o uses non-blocking I/O calls
o can handle thousands of concurrent
connections
● Unified API
o use the same services and models on
both server and client side
19. Under the hood
Coding for Kombol | Riyadh Al Nur | Objects and Functions
Image credit Toptal Engineering blog
20. Under the hood
Coding for Kombol | Riyadh Al Nur | Objects and Functions
● Event loop
o uses event loops instead of
processes and threads
o no explicit calls required - callbacks,
callbacks, callbacks!
21. The dark side
Coding for Kombol | Riyadh Al Nur | Objects and Functions
● Single thread = not good for heavy
computation
● App depends on a relational DB
22. Creating a simple HTTP server
Coding for Kombol | Riyadh Al Nur | Objects and Functions
var http = require(‘http’);
http.createServer(function (req, res) {
res.writeHead(200,
{‘Content-Type’: ‘text/plain’});
res.end(‘Hello World!n’);
}).listen(80);
23. ExpressJS
Coding for Kombol | Riyadh Al Nur | Objects and Functions
var express = require(‘express’);
// that easy
● A NodeJS web application framework
● Makes the previous example simpler but
more robust and powerful