06 Javascript
06 Javascript
Javascript
Further(reading:(
h:p://eloquentjavascript.net/(
h:ps://developer.mozilla.org/enHUS/docs/Web/JavaScript(
(italiano)(h:ps://developer.mozilla.org/it/docs/Web/JavaScript(
(
• JavaScript(was(introduced(by(Netscape(in(their(
Navigator(browser(back(in(1996.(
• It(was(originally(going(to(be(called(LiveScript,(but(it(
was(renamed(to(capitalize(on(the(popularity(of(Sun(
Microsystem's(Java(language(
• JavaScript(is(in(fact(an(implementaOon(of(a(
standardized(scripOng(language(called(ECMAScript,,
today,at,6th,edi4on,(June,2015),
• ObjectRoriented(dynamic(language((
• types(and(operators,(standard(builtRin(objects,(and(
methods.((
• Syntax(is(based(on(the(Java(and(C(languages(
• many(structures(from(those(languages(apply(to(
JavaScript(as(well(
• Designed(to(run(as(a(scripOng(language(in(a(host(
environment(
• Browser,(Node.js,(database(CouchDB,(embedded(
computers,(GNOME,(Adobe(Photoshop,(ect.(
There(are(many(advantages(of(clientRside(scripOng:(
• Processing(can(be(offloaded(from(the(server(to(
client(machines,(thereby(reducing(the(load(on(the(
server.((
• The(browser(can(respond(more(rapidly(to(user(
events(than(a(request(to(a(remote(server(ever(
could,(which(improves(the(user(experience.(
• JavaScript(can(interact(with(the(downloaded(HTML(
in(a(way(that(the(server(cannot,(creaOng(a(user(
experience(more(like(desktop(so[ware(than(simple(
HTML(ever(could.(
JavaScript(can(be(linked(to(an(HTML(page(in(a(number(
of(ways.(
• Inline(
• Embedded(
• External(
Inline(JavaScript(refers(to(the(pracOce(of(including(
JavaScript(code(directly(within(certain(HTML(a:ributes(
Inline(JavaScript(is(a(real(maintenance(nightmare(
Embedded(JavaScript(refers(to(the(pracOce(of(placing(
JavaScript(code(within(a(<script>(element(
JavaScript(supports(this(separaOon(by(allowing(links(to(
an(external(file(that(contains(the(JavaScript.(
By(convenOon,(JavaScript(external(files(have(the(
extension(.js.(
Numbers
For small or big numbers you can use
scientific notation:
2.998e8 = 2.998 × 108 = 299,800,000
Calculations with fractional numbers are
generally not precise given the 64 bit limit.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operator
s/Operator_Precedence
3 Special Numbers
> 0/1 Infinity and -Infinity
< 0
> 1/0 Infinity - 1 is still Infinity
< Infinity NaN stands for “not a number”
> 0/0
< NaN Any number of other numeric
> Infinity - 1 operations that don’t yield a precise,
< Infinity meaningful result.
Strings
Strings are text wrapped in quotes.
> true ? 1 : 2
< 1
> false ? 1 : 2
< 2
Undefined Values
There are two special values, null and undefined, that
are used to denote the absence of a meaningful value.
Many operations in the language that don’t produce a
meaningful value (you’ll see some later) yield undefined
simply because they have to yield some value.
> 8 * null
< 0
> "5" - 1
< 4
> "5" + 1
< "51"
> "five" * 2
< NaN
> false == 0
< true
> 1;
> !false;
var input;
if(input === undefined){
doThis();
} else {
doThat();
}
Variable scope
Variables outside any function are called global, because
available to any code.
Variables declared within a function (using var) are local
because available only to that function
Javascript does not have block statement scope like in java
if (true) {
var x = 5;
}
console.log(x); // 5
window.x = 1;
console.log(x); // 1
console.log(x == window.x); // true
Global variables are part of the global object. In web pages the
global object is window, so you can set and access global
variables using the window.variable syntax.
Variable hoisting
Variable can be referred even if declared later.
Variables are "lifted" to the top of the function or statement, and
initialized as undefined
var i = -1;
i = -1;
y 1234
addr: 0x183da5
bar ref:0x48264e
An object is a
collection of properties
A property is a
named container for a value
w/ some additional attributes
Definition
y w addr: 0x2f4b91
y 1234
var day = {
events: events,
squirrel: false
};
console.log(day.events);
// → ["work","television"]
console.log(screen.squirrel);
// → false
console.log(events.length);
// → 2
Properties - bracket
When using square brackets, the expression between the
brackets is evaluated to get the property name.
"2" and "john doe" aren't valid variable names so they can't
be accessed through the dot notation.
var random = {
1: "one"
"2": 2
"john doe": "unknown"
};
var x = 1;
console.log(random[x])
// → "one"
console.log(random["2"])
// → 2
console.log(random["john doe"]);
// → "unknown"
Add/Get/Set/Remove A Property
We can dynamically modify an object after its creation
var obj = {
1 : "Hello",
"3": "Good",
x : "JavaScript",
foo: 101,
bar: true,
"" : null
};
addr: 0x440892
var x = { a: 100 }; x
var y = { a: 100 }; ref:0x440892 a 100
addr: 0x3c980c
y
ref:0x3c980c a 100
addr: 0x440892
var x = { a: 100 }; x
var y = { a: 100 };
var z = y;
ref:0x440892 a 100
x === y; // false
y === z; // true addr: 0x3c980c
y
ref:0x3c980c a 100
z
ref:0x3c980c
Don't Forget Any Value Of The
Object Type Is Actually A “Reference”
addr: 0x440892
var x = { a: 100 }; x
var y = { a: 100 };
var z = y;
ref:0x440892 a 100
x === y; // false
y === z; // true addr: 0x3c980c
y
ref:0x3c980c a 200
z.a = 200;
z
x.a; // 100 ref:0x3c980c
y.a; // 200
z.a; // 200
Definition
A function is an object
that is callable
Functions
console.log
Most JavaScript systems (including all modern web browsers
and Node.js) provide a console.log function that writes out its
arguments to some text output device
var x = 30;
console.log("the value of x is", x);
// → the value of x is 30
console.log(square(12));
// → 144
var a = 7;
var sayhi = function (name) {
var a = "Hello " + name;
console.log(a);
return a;
};
var f2 = function() {
x = "inside f2";
};
f2();
console.log(x); // → inside f2
flat(3);
mountain(4);
flat(6);
mountain(1);
flat(1);
return result;
};
console.log(landscape());
// → ___/''''\______/'\_
When # of Arguments Not Matched
In JavaScript, it is OK to invoke a function
with a mismatched number of arguments.
At run time, any argument without passing a value is filled with the
“undefined” value, just like a local variable without any assignment.
var f = function () {
return;
};
var g = function () {
};
var x = 100;
plusOne(x); // 101
plusOne(y); // NaN
var y = 999;
x = 100;
plusOne(x); // 101
plusOne(y); // NaN
y = 999;
• JavaScript(is(a(prototypeRbased(language(and(
contains(no(class(statement(
• JavaScript(uses(funcOons(as(constructors(for(
classes.(Defining(a(class(is(as(easy(as(defining(a(
funcOon(
//constructor
//instances
new(is(strongly(related(
to(this.(It(creates(a(new(
empty(object,(and(then(
calls(the(funcOon(
specified,(with(this(set(
to(that(new(object.(
Each,method,
invoca4on,creates,a,
new,func4on,object!(
A(number(of(useful(objects(are(included(with(
JavaScript(including:(
• Array(
• Date(
• Math(
• String,(Number,(Boolean(((primiOve(wrappers)(
• Dom(objects(
To(add(an(item(to(an(exisOng(array,(you(can(use(the(
push(method.(
greeOngs.push("Good(Evening");(
The(pop(method(can(be(used(to(remove(an(item(from(
the(back(of(an(array.(
AddiOonal(methods:(concat(),(slice(),(join(),(reverse(),(
shi[(),(and(sort()(
The(Math,class,allows(one(to(access(common(
mathemaOc(funcOons(and(common(values(quickly(in(
one(place.(
Contains(methods(such(as(max(),(min(),(pow(),(sqrt(),(
and(exp(),(and(trigonometric(funcOons(such(as(sin(),(
cos(),(and(arctan().(
Many(mathemaOcal(constants(are(defined(such(as(PI,(
E,(SQRT2,(and(some(others(
Math.PI;(//"3.141592657"
Math.sqrt(4);,//"square"root"of"4"is"2."
Math.random();,//"random"number"between"0"and"1(
It(allows(you(to(quickly(calculate(the(current(date(or(create(date(
objects(for(parOcular(dates.((
To(display(today’s(date(as(a(string,(we(would(simply(create(a(
new(object(and(use(the(toString()(method.(
var(d(=(new,Date();,
//"This"outputs"Today"is"Mon"Nov"12"2012"15:40:19"GMTE0700"
alert(("Today(is("+(d.toString());,
The(window(object(in(JavaScript(corresponds(to(the(browser(
itself.(Through(it,(you(can(access(the(current(page’s(URL,(the(
browser’s(history,(and(what’s(being(displayed(in(the(status(bar,(
as(well(as(opening(new(browser(windows.((
In(fact,(the(alert()(funcOon(menOoned(earlier(is(actually(a(
method(of(the(window(object.,