Top Banner
JavaScript Basics Adam Crabtree (Adapted from Joar Gullestad Pettersen’s http://slideshare.net/Peanuts_Stavanger/javascript-basics-29353026)
51

LinkedIn TBC JavaScript 100: Intro

Jul 15, 2015

Download

Technology

Adam Crabtree
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: LinkedIn TBC JavaScript 100: Intro

JavaScript BasicsAdam Crabtree

(Adapted from Joar Gullestad Pettersen’s

http://slideshare.net/Peanuts_Stavanger/javascript-basics-29353026)

Page 2: LinkedIn TBC JavaScript 100: Intro

JavaScript Data Types

String, Number, Boolean, Array, Object, Null, Undefined

Page 3: LinkedIn TBC JavaScript 100: Intro

Dynamic Types

• var x;

Page 4: LinkedIn TBC JavaScript 100: Intro

Dynamic Types

var x; // x is: undefined

Page 5: LinkedIn TBC JavaScript 100: Intro

Dynamic Types

var x; // x is: undefined

x = 5; // x is set to a Number: 5

Page 6: LinkedIn TBC JavaScript 100: Intro

Dynamic Types

var x; // x is: undefined

x = 5; // x is set to a Number: 5

x = "Bond"; // x is changed to a String: "Bond"

Page 7: LinkedIn TBC JavaScript 100: Intro

Strings

var car = "Telsa Model S";

var car2 = 'Tesla Model S';

Page 8: LinkedIn TBC JavaScript 100: Intro

Numbers

• var x = 42; // Written without decimals

• var y = 13.37; // Written with decimals

• var z = 10e3; // Exponential notation

Page 9: LinkedIn TBC JavaScript 100: Intro

Booleans

• var x = true;

• var y = false;

Page 10: LinkedIn TBC JavaScript 100: Intro

Array

var frukt = new Array();

frukt[0] = "eple";

frukt[1] = "banan";

frukt[2] = "pære";

["eple", "banan", "pære"]

Page 11: LinkedIn TBC JavaScript 100: Intro

Array 2

var frukt = new Array("eple", "banan", "pære");

["eple", "banan", "pære"]

Page 12: LinkedIn TBC JavaScript 100: Intro

Array 3

var frukt = ["eple", "banan", "pære"];

["eple", "banan", "pære"]

Page 13: LinkedIn TBC JavaScript 100: Intro

Array 4

• var frukt = ["eple", "banan", "pære"]

• frukt.pop(); // ["eple", "banan"]

• frukt.push("tomat"); // ["eple", "banan", "tomat"]

• frukt.shift(); // ["banan", "tomat"]

• frukt.unshift("tomat"); // ["tomat", "banan", "tomat"]

Page 14: LinkedIn TBC JavaScript 100: Intro

Objects

• Everything is an Object

Page 15: LinkedIn TBC JavaScript 100: Intro

Objects

• Everything is an Object

• Booleans can be objects or primitive data treated as objects

• Numbers can be objects or primitive data treated as objects

• Strings are also objects or primitive data treated as objects

• Dates, Maths, Regular expressions, Arrays and functions are always objects

Page 16: LinkedIn TBC JavaScript 100: Intro

Object literalAn object is just a special kind of

data, with properties andmethods.

var person = {

firstName: "John",

lastName: "Doe",

id: 5

};

Page 17: LinkedIn TBC JavaScript 100: Intro

Object literalAn object is just a special kind of data,

with properties and methods.

var person = {

firstName: "John",

lastName: "Doe",

id: 5

};

person.id; // 5

Page 18: LinkedIn TBC JavaScript 100: Intro

Object literalAn object is just a special kind of data,

with properties and methods.

var person = {

firstName: "John",

lastName: "Doe",

address: {

street: "Strandsvingen",

number: "14B"

}

};

person.address.street; // "Strandsvingen"

Page 19: LinkedIn TBC JavaScript 100: Intro

Functionsa block of code that will be executed when "someone" calls it:

function add(a, b) {

return a + b;

}

var add = function(a, b) {

return a + b;

}

Page 20: LinkedIn TBC JavaScript 100: Intro

Object Constructor

function Person(firstName, lastName) {

this.firstName = firstName;

this.lastName = lastName;

}

var randomPerson = new Person("John", "Doe");

Page 21: LinkedIn TBC JavaScript 100: Intro

Object

var randomPerson = new Object();

randomPerson.firstName = "John";

randomPerson.lastName = "Doe";

Page 22: LinkedIn TBC JavaScript 100: Intro

Boolean expressions

if (a == 2) {//if this is true

//do this...

}

Page 23: LinkedIn TBC JavaScript 100: Intro

Type coercion

• When JavaScript are unsure what you mean, It makes a guess

• Example:

if ('false') { console.log("true"); }

• «A String is obviously not true or false, you probablymean true!»

Page 24: LinkedIn TBC JavaScript 100: Intro

True!

• if (true) { alert("true"); } // alerts "true"

• if ('false') { alert("true"); } // alerts "true"

• if ([]) { alert("true"); } // alerts "true"

• if (5) { alert("true"); } // alerts "true"

• if ({}) { alert("true"); } // alerts "true"

Page 25: LinkedIn TBC JavaScript 100: Intro

False

• if (false) { alert("false"); } // does nothing

• if (0) { alert("false"); } // does nothing

• if (null) { alert("false"); } // does nothing

• if (undefined) { alert("false"); } // does nothing

• if ("") { alert("false"); } // does nothing

Page 26: LinkedIn TBC JavaScript 100: Intro

More type coercion

1 == "1"

true == "1"

false == "0"

Page 27: LinkedIn TBC JavaScript 100: Intro

More type coercion

1 == "1"

true

true == "1"

false == "0"

Page 28: LinkedIn TBC JavaScript 100: Intro

More type coercion

1 == "1"

true

true == "1"

true

false == "0"

Page 29: LinkedIn TBC JavaScript 100: Intro

More type coercion

1 == "1"

true

true == "1"

true

false == "0"

true

Page 30: LinkedIn TBC JavaScript 100: Intro

More type coercion

1 == "1"

true

true == "1"

true

false == "0"

truefalse == "0" == 1 == true == [] == ""

Page 31: LinkedIn TBC JavaScript 100: Intro

More type coercion

1 == "1"

true

true == "1"

true

false == "0"

truefalse == "0" == 1 == true == [] == ""

true

Page 32: LinkedIn TBC JavaScript 100: Intro

Confusing?

• Solution?

Page 33: LinkedIn TBC JavaScript 100: Intro

Confusing?

• Solution?

• Avoid bugs| and use: ===

Page 34: LinkedIn TBC JavaScript 100: Intro

===

1 == true

true

1 === true

false

1 == "1"

true

1 === "1"

false

Page 35: LinkedIn TBC JavaScript 100: Intro

Scope & global variables

• C#/Java: anything inside curly brackets, {} , defines a scope

• Example:

if (true) {

var scopeVariable = "Test";

}

scopeVariable = "Test2"; // variable not defined

Page 36: LinkedIn TBC JavaScript 100: Intro

Scope & global variables

• Javascript: only functions define a new scope

• Example:

if (true) {

var scopeVariable = "Test";

}

scopeVariable; // value: "Test";

Page 37: LinkedIn TBC JavaScript 100: Intro

Scope & global variables

function scope1() {

var member; //is a member of the scope defined by the function example

//this function is also part of the scope of the function example

var innerScope = function() {

member= 12; // traverses scope and assigns member in scope1 to 12

};

};

Page 38: LinkedIn TBC JavaScript 100: Intro

Scope & global variables

function scope1() {

var member; //is a member of the scope defined by the function example

//this function is also part of the scope of the function example

var innerScope = function() {

var member= 12; // defines member in this scope and do not traverse

};

};

Page 39: LinkedIn TBC JavaScript 100: Intro

Scope & global variables

function scope1() {

var member; //is a member of the scope defined by the function example

//this function is also part of the scope of the function example

var bar = function() {

member= 12; // traverses scope and assigns scope1member.foo to 12

};

};

function scope2() {

member = 15; // traverses scope and assigns global.member to 15

}

Page 40: LinkedIn TBC JavaScript 100: Intro

Namespaces

• Not built into JavaScript

• Problem?

Page 41: LinkedIn TBC JavaScript 100: Intro

JavaScript (Ad-hoc) namespace

var Peanuts = {}; // Object used as namespace

Page 42: LinkedIn TBC JavaScript 100: Intro

JavaScript (Ad-hoc) namespace

var Peanuts = Peanuts || {}; // in case it alreadyexists

Page 43: LinkedIn TBC JavaScript 100: Intro

«Classes» and «methods» ?

var Peanuts = Peanuts || {};

Peanuts.Calculator = {

add: function (a,b) {

return a + b;

},

subtract: function () {

return a - b;

}

};

Peanuts.Calculator.add(1, 2); // 3

Page 44: LinkedIn TBC JavaScript 100: Intro

Immediately invoked function expressions

• (function () {

• // logic/code here

• })();

Page 45: LinkedIn TBC JavaScript 100: Intro

Why?

• Executes an expression and therefore code immediately

• Creates «privacy» for your code (function scope ftw!)

Page 46: LinkedIn TBC JavaScript 100: Intro

How?

• JavaScript, parenthesis can’t contain statements.

• When the parser encounters the function keyword, it knows to

parse it as a function expression and not a function declaration.

Page 47: LinkedIn TBC JavaScript 100: Intro

Immediately invoked function expressions

• (function () {

• // logic/code here

• })();

• The key thing about JavaScript expressions is that they return values.

• To invoke the function expression right away we just need to tackle a couple

of parentheses on the end(like above).

Page 48: LinkedIn TBC JavaScript 100: Intro

Immediately invoked function expressions

• (function (innerValue) {

• // logic/code here

• })(outerValue);

Page 49: LinkedIn TBC JavaScript 100: Intro

Revealing module pattern

var Peanuts = Peanuts || {};

Peanuts.Calculator = function () {

var add = function(a, b) {

return a + b;

};

var subtract = function(a, b) {

return a - b;

};

return {

add: add,

subtract: subtract

};

};

Peanuts.Calculator().add(1, 2); // 3

Page 50: LinkedIn TBC JavaScript 100: Intro

Revealing module pattern 2

var Peanuts = Peanuts || {};

Peanuts.Calculator = (function () {

var add = function(a, b) {

return a + b;

};

return {

add: add,

};

})();

Peanuts.Calculator.add(1, 2); // 3

Page 51: LinkedIn TBC JavaScript 100: Intro

Revealing module pattern 3

var Peanuts = Peanuts || {};

Peanuts.Calculator = (function () {

var PI = 3.14; // private variable!

var getCircleArea = function(r) {

return PI * r * r;

};

return {

getCircleArea: getCircleArea // public method

};

})();

Peanuts.Calculator.getCircleArea(2); // 12.56