what js? itself

Post on 05-Apr-2017

18 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

What JavaScript?Remove your whats

of JavaScript

The Creation

Brendan Eich

Made JavaScript in 10 days.Hence..

JavaScript

Simple to use.

It really doesn’t care

Semicolons are optional

Parameters may or may not be passed

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

It really doesn’t care

Order of execution isn’t the order written

It really doesn’t caredoSomething()

function doSomething() {

console.log( ‘doing something’ )

}

// This do works..

Weakly typed

All variables may contain anything.

var something = 5;

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

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

something = sayHi();

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

Weakly typed

Weakly typed

Operations between types can be executed

Conversion on the fly will occur

Weakly typedvar firstValue = 0;

firstValue++; // increase the integer

console.log(firstValue); // shows 1

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

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

Discovering JS

Types

Conditionals

Types Conversion

Scope

Types

Undefined

Null

Boolean

String

Number

Object including Array

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.

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

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

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”);

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”);

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

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

Discovering JS

Types

Conditionals

Types Conversion

Scope

Conditionals

With or without brackets conditionals must be understood.

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.

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.

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.

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]!

Conditionals

ALWAYS USE 3 EQUALS

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

Types conversion

“”+1234

Types conversion

“1234”

Types conversion

+“1.357”

Types conversion

1.357

Types conversion

!!“gaga”

Types conversion

true

Types conversion

!!0

Types conversion

false

Types conversion

!!undefined

Types conversion

false

Types conversion

!!dogs.length

Types conversion

false/true

Discovering JS

Types

Conditionals

Types Conversion

Scope

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.

Scope

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.

Nested Scopes

top related