what js? itself

48
What JavaScript? Remove your whats of JavaScript

Upload: lucio-martinez

Post on 05-Apr-2017

17 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: What JS? Itself

What JavaScript?Remove your whats

of JavaScript

Page 2: What JS? Itself

The Creation

Page 3: What JS? Itself

Brendan Eich

Page 4: What JS? Itself

Made JavaScript in 10 days.Hence..

Page 5: What JS? Itself

JavaScript

Simple to use.

Page 6: What JS? Itself

It really doesn’t care

Semicolons are optional

Parameters may or may not be passed

Page 7: What JS? Itself

It really doesn’t carefunction getMessage(message) {

internalVariable = “The message is: ”

return internalVariable + message

}

var result = getMessage(“dogs are nice”, 123, undefined, “etc”)

console.log( result )// this’d positively print “The message is: dogs are nice” without errors

Page 8: What JS? Itself

It really doesn’t care

Order of execution isn’t the order written

Page 9: What JS? Itself

It really doesn’t caredoSomething()

function doSomething() {

console.log( ‘doing something’ )

}

// This do works..

Page 10: What JS? Itself

Weakly typed

All variables may contain anything.

Page 11: What JS? Itself

var something = 5;

console.log(something); // shows a 5

function sayHi() { return “Hi!”;}

something = sayHi();

console.log(something); // shows hi!

Weakly typed

Page 12: What JS? Itself

Weakly typed

Operations between types can be executed

Conversion on the fly will occur

Page 13: What JS? Itself

Weakly typedvar firstValue = 0;

firstValue++; // increase the integer

console.log(firstValue); // shows 1

firstValue += “1”; // concatenates 1 with “1”

console.log(firstValue); // shows “11”

Page 14: What JS? Itself

Discovering JS

Types

Conditionals

Types Conversion

Scope

Page 15: What JS? Itself

Types

Undefined

Null

Boolean

String

Number

Object including Array

Page 16: What JS? Itself

Type undefined

People say that undefined may change the value. I don’t care about that.

if (newVariable === undefined)

This is perfectly fine for my standards.

(function(undefined) {

console.log(undefined); // Prints the given string, my own undefined

})(“my own undefined”);

This is stupid. So I don’t care about undefined not being undefined.

Page 17: What JS? Itself

Type null

Very common value in other languages.

Can be seen a lot retrieving data from APIs.

var received = null;

if (received === null) {} // True

if (!received) {} // True too

Page 18: What JS? Itself

Type boolean

var that = true;

if (that === true) {} // This is silly

if (that) {} // This is better

if (that == false) {} // Same as the first, silly

if (!that) {} // This is great

Page 19: What JS? Itself

Type string

var text = “There was a time”;

if (text != “”)

console.log(“Text is not empty”);

// Above code is far too silly, it can be simpler as below

if (text)

console.log(“Text is not empty”);

Page 20: What JS? Itself

Type number

var luckNumber = 5;

if (luckNumber != 0)

console.log(“Number is not zero”);

// This can be easier:

if (luckNumber)

console.log(“Number is positive or negative, not zero”);

Page 21: What JS? Itself

Type object

// Right way to initialize an object, not using new

var virtualObject = {};

if (virtualObject)

console.log(“Object exist”); // Even without attributes, it exist

virtualObject.firstAttribute = 1234; // Add attribute

if (virtualObject.unexistingMethod) // Continue only if method available

virtualObject.unexistingMethod(); // Unknown method error prevented

Page 22: What JS? Itself

Type object

var dogs = [];

if (dogs) // Object actually exist

dogs.push(“Terry”); // Now has one element

console.log(dogs); // Prints [“Terry”]

If (dogs.length) // Length won’t be zero

console.log(“Array has elements”); // So this actually prints

Page 23: What JS? Itself

Discovering JS

Types

Conditionals

Types Conversion

Scope

Page 24: What JS? Itself

Conditionals

With or without brackets conditionals must be understood.

Page 25: What JS? Itself

Conditionals

if (typeof aName == “string” && aName != “”) {

console.log(“Say hello to ” + aName);

}

This is wrong, javascript has a conditional of 3 equals.

Do NEVER use the 2 equals.

2 sometimes are OK, 3 will be always OK. Use 3 equals always.

Page 26: What JS? Itself

Conditionals

if (typeof aName === “string” && aName !== “”) {

console.log(“Say hello to ” + aName);

}

Now this is still pretty awful. If aName is integer 18 then you cannot print hello to 18.

Page 27: What JS? Itself

Conditionals

if (aName !== “”) {

console.log(“Say hello to ” + aName);

}

Yet horrible, there is no need to ask for not empty string explicitly when it can be simpler.

Page 28: What JS? Itself

Conditionals

// From `if (typeof aName == “string” && aName != “”)`

// To

if (aName) {

console.log(“Say hello to ” + aName);

}

Simplicity is on our side. Take that [INSERT CORRECT STARS WAR CHARACTER]!

Page 29: What JS? Itself

Conditionals

ALWAYS USE 3 EQUALS

Page 30: What JS? Itself

Types Conversion

Now we are entering to the fun side

JavaScript is well known for its types conversion table

Also for its logic tables

And for everything else, but not in the good way

More as a joke

So lets try to get a good laugh from it

Page 31: What JS? Itself

Types conversion

“”+1234

Page 32: What JS? Itself

Types conversion

“1234”

Page 33: What JS? Itself

Types conversion

+“1.357”

Page 34: What JS? Itself

Types conversion

1.357

Page 35: What JS? Itself

Types conversion

!!“gaga”

Page 36: What JS? Itself

Types conversion

true

Page 37: What JS? Itself

Types conversion

!!0

Page 38: What JS? Itself

Types conversion

false

Page 39: What JS? Itself

Types conversion

!!undefined

Page 40: What JS? Itself

Types conversion

false

Page 41: What JS? Itself

Types conversion

!!dogs.length

Page 42: What JS? Itself

Types conversion

false/true

Page 43: What JS? Itself

Discovering JS

Types

Conditionals

Types Conversion

Scope

Page 44: What JS? Itself

Scope

We can define a scope as a set of rules for storing variables in some location, and for finding those variables at a later time.

Page 45: What JS? Itself

Scope

Page 46: What JS? Itself

Nested Scopes

JavaScripts starts at the currently executing scope, looks for the variable there, then if not found, keeps going up one level, and so on. If the outermost global scope is reached, the search stops, whether it finds the variable or not.

Page 47: What JS? Itself

Nested Scopes

Page 48: What JS? Itself