JAVASCRIPT - GOOD PARTS
Manohar Shetty || Supriya Anand
SESSION-1
Efforts put in to make a robot called Javascript
SESSION-2
Play with javascipt - alerts, console.logs and what not.
SESSION-2
Frustated! Why somethings work and somethings don't?
I am bad... very very bad.
GLOBAL VARIABLES
No linker. Global namespace. Variable collisions!
//Your code
function foo() {
if (false) {
var x = 1;
}
return;
var y = 1;
}
//How Javascript interprets it
function foo() {
var x, y;
if (false) {
x = 1;
}
return;
y = 1;
}
'+'
Adds and concatenates.
Un-predictable and error prone
0 + "1"
// returns "01"
"3" + 1
// returns "31"
"3" - "2"
// returns 1
SEMICOLON INSERTION
Semi-colons are INSERTED by the parser
Optional semi-colons are not a “language feature”
You are not warned!! .... 'Uh oh..'
“JavaScript: where forgetting the occasional
semicolon isn't necessarily the end of the
world.”
NOT WHAT YOU EXPECT
TYPEOF()
typeof('abc’) -> string -> Nice
typeof(1) -> number -> Good
typeof({}) -> object -> Great
typeof([1,2,3]) -> object -> Ooops
typeof(null) -> object -> WTH
AGAIN..
== AND !=
‘ ’ == 0 -> false
0 == ‘ ‘ -> true
false == ‘false’ -> false
false == ‘0’ -> true
false == undefined -> false
undefined == null -> true
FLOATING POINTS
0.1 + 0.2 = 0.3 //Nope, back to grade 1 you go!
> 0.1 + 0.2 == 0.3
false
> 0.1 + 0.2 = 0.30000000000000004
WHAT?!
STYLE IS NOT SUBJECTIVE
return {
status: ‘OK’
}
return
{
status: ‘OK’
}
return;
{
status: ‘OK’;
}
OKAY, ENOUGH OF THE BAD!
Show me the good!!
FINALLY, LETS TALK ABOUT THE GOOD.
OBJECT LITERALS
var obj = {
name: 'manohar',
age: 23
}
Pass as options to functions
JSON for communication, remember API's anyone?
DYNAMIC OBJECTS
Members can be added to any object, any time
Helpful to attach attributes
var obj = {
count: 2,
getCount: function(){
return this.count;
}
}
obj.incrementCount = function(){
this.count++;
}
obj.incrementCount();
alert(obj.getCount()); // 3
LOOSELY TYPED
/* JavaScript Example (loose typing) */
var a = 13; // Number declaration
var b = "thirteen"; // String declaration
b=a // b = 13
/* Java Example (strong typing) */
int a = 13; // int declaration
String b = "thirteen"; // String declaration
FUNCTIONS ARE OBJECTS
can be passed, returned
var fibonacci = function(number) {
number < 2 ? number : fibonacci(number -1 ) + fibonacci(number - 2)
}
LAMBDA / CLOSURES
a closure is a reference to the local vars for a function -
local vars kept alive after the function has returned
a closure is a stack-frame which is not deallocated when
the function returns. (as if a 'stack-frame' were malloc'ed
instead of being on the stack!)
function, inner function, outer function, function inside
function, this function, that function
CONFUSED?
CLOSURES, IN ACTION
var wishPeople = function(){
var names = ['manohar', 'supriya', 'hari', 'DP'];
var wish = function(){
for(i=0, n=names.length; i < n;i++) {
console.log("Hello!" + names[i] + ". Welcome to Women Who Code meetup
}
};
return wish;
}();
wishPeople();
Prototypal Inheritance
Memoization
Callbacks
scope
AND MORE!
www.mavenhive.in

Javascript good parts - for novice programmers

  • 1.
    JAVASCRIPT - GOODPARTS Manohar Shetty || Supriya Anand
  • 2.
    SESSION-1 Efforts put into make a robot called Javascript
  • 3.
    SESSION-2 Play with javascipt- alerts, console.logs and what not.
  • 4.
    SESSION-2 Frustated! Why somethingswork and somethings don't?
  • 5.
    I am bad...very very bad.
  • 6.
    GLOBAL VARIABLES No linker.Global namespace. Variable collisions! //Your code function foo() { if (false) { var x = 1; } return; var y = 1; } //How Javascript interprets it function foo() { var x, y; if (false) { x = 1; } return; y = 1; }
  • 7.
    '+' Adds and concatenates. Un-predictableand error prone 0 + "1" // returns "01" "3" + 1 // returns "31" "3" - "2" // returns 1
  • 8.
    SEMICOLON INSERTION Semi-colons areINSERTED by the parser Optional semi-colons are not a “language feature” You are not warned!! .... 'Uh oh..' “JavaScript: where forgetting the occasional semicolon isn't necessarily the end of the world.”
  • 9.
    NOT WHAT YOUEXPECT TYPEOF() typeof('abc’) -> string -> Nice typeof(1) -> number -> Good typeof({}) -> object -> Great typeof([1,2,3]) -> object -> Ooops typeof(null) -> object -> WTH
  • 11.
    AGAIN.. == AND != ‘’ == 0 -> false 0 == ‘ ‘ -> true false == ‘false’ -> false false == ‘0’ -> true false == undefined -> false undefined == null -> true
  • 13.
    FLOATING POINTS 0.1 +0.2 = 0.3 //Nope, back to grade 1 you go! > 0.1 + 0.2 == 0.3 false > 0.1 + 0.2 = 0.30000000000000004 WHAT?!
  • 14.
    STYLE IS NOTSUBJECTIVE return { status: ‘OK’ } return { status: ‘OK’ } return; { status: ‘OK’; }
  • 15.
    OKAY, ENOUGH OFTHE BAD! Show me the good!!
  • 16.
    FINALLY, LETS TALKABOUT THE GOOD.
  • 17.
    OBJECT LITERALS var obj= { name: 'manohar', age: 23 } Pass as options to functions JSON for communication, remember API's anyone?
  • 18.
    DYNAMIC OBJECTS Members canbe added to any object, any time Helpful to attach attributes var obj = { count: 2, getCount: function(){ return this.count; } } obj.incrementCount = function(){ this.count++; } obj.incrementCount(); alert(obj.getCount()); // 3
  • 19.
    LOOSELY TYPED /* JavaScriptExample (loose typing) */ var a = 13; // Number declaration var b = "thirteen"; // String declaration b=a // b = 13 /* Java Example (strong typing) */ int a = 13; // int declaration String b = "thirteen"; // String declaration
  • 20.
    FUNCTIONS ARE OBJECTS canbe passed, returned var fibonacci = function(number) { number < 2 ? number : fibonacci(number -1 ) + fibonacci(number - 2) }
  • 21.
    LAMBDA / CLOSURES aclosure is a reference to the local vars for a function - local vars kept alive after the function has returned a closure is a stack-frame which is not deallocated when the function returns. (as if a 'stack-frame' were malloc'ed instead of being on the stack!) function, inner function, outer function, function inside function, this function, that function CONFUSED?
  • 22.
    CLOSURES, IN ACTION varwishPeople = function(){ var names = ['manohar', 'supriya', 'hari', 'DP']; var wish = function(){ for(i=0, n=names.length; i < n;i++) { console.log("Hello!" + names[i] + ". Welcome to Women Who Code meetup } }; return wish; }(); wishPeople();
  • 23.
  • 25.