obvious secrets of javascript

52
JavaScript obvious secrets of Monday, 21 December 2009

Upload: dmitry-baranovskiy

Post on 06-May-2015

3.352 views

Category:

Technology


5 download

TRANSCRIPT

Page 1: Obvious Secrets of JavaScript

JavaScriptobvious secrets of

Monday, 21 December 2009

Page 2: Obvious Secrets of JavaScript

Monday, 21 December 2009

Page 3: Obvious Secrets of JavaScript

Every object (including host objects) must implement the

((Prototype)) and ((Class)) properties and the ((Get)), ((Put)),

((CanPut)), ((HasProperty)), ((Delete)), and ((DefaultValue))

methods. (Note, however, that the ((DefaultValue)) method may,

for some objects, simply throw a TypeError exception.) The value

of the ((Prototype)) property must be either an object or null, and

every ((Prototype)) chain must have finite length (that is, starting

from any object, recursively accessing the ((Prototype)) property

must eventually lead to a null value). Whether or not a native

object can have a host object as its ((Prototype)) depends on the

implementation. The value of the ((Class)) property

is defined by this specification for every

kind of built-in object. The value of the

((Class)) property of a host object may

be any value, even a value used by Monday, 21 December 2009

Page 4: Obvious Secrets of JavaScript

Every object (including host objects) must implement the

((Prototype)) and ((Class)) properties and the ((Get)), ((Put)),

((CanPut)), ((HasProperty)), ((Delete)), and ((DefaultValue))

methods. (Note, however, that the ((DefaultValue)) method may,

for some objects, simply throw a TypeError exception.) The value

of the ((Prototype)) property must be either an object or null, and

every ((Prototype)) chain must have finite length (that is, starting

from any object, recursively accessing the ((Prototype)) property

must eventually lead to a null value). Whether or not a native

object can have a host object as its ((Prototype)) depends on the

implementation. The value of the ((Class)) property

is defined by this specification for every

kind of built-in object. The value of the

((Class)) property of a host object may

be any value, even a value used by

It is a bit “cryptic”

Monday, 21 December 2009

Page 5: Obvious Secrets of JavaScript

Types

Monday, 21 December 2009

Page 6: Obvious Secrets of JavaScript

number

boolean

null

string

object

undefined

Monday, 21 December 2009

Page 7: Obvious Secrets of JavaScript

typeof

undefined "undefined"

null "object"

number "number"

boolean "boolean"

string "string"

object "object"

Monday, 21 December 2009

Page 8: Obvious Secrets of JavaScript

to Number

undefined NaN

null 0

number —

boolean true→1, false→0

string parsing

object .valueOf()

Monday, 21 December 2009

Page 9: Obvious Secrets of JavaScript

to String

undefined "undefined"

null "null"

number "5"

boolean "true"

string —

object .toString()

Monday, 21 December 2009

Page 10: Obvious Secrets of JavaScript

to Boolean

undefined FALSE

null FALSE

number 0||NaN→false

boolean —

string ""→false

object TRUE

Monday, 21 December 2009

Page 11: Obvious Secrets of JavaScript

to Object

undefined exception!

null exception!

number new Number(5)

boolean new Boolean(true)

string new String("js")

object object

Monday, 21 December 2009

Page 12: Obvious Secrets of JavaScript

5 + 4 + "px""$" + 1 + 2"4" / "2""$" + 1 - 2"web".lengthtypeof 5typeof "5"typeof {key: value}typeof nulltypeof undefinedtypeof [1, 2, 3]typeof (4 - "px")+!{}[0]

Monday, 21 December 2009

Page 13: Obvious Secrets of JavaScript

5 + 4 + "px""$" + 1 + 2"4" / "2""$" + 1 - 2"web".lengthtypeof 5typeof "5"typeof {key: value}typeof nulltypeof undefinedtypeof [1, 2, 3]typeof (4 - "px")+!{}[0]

"9px""$12"2NaN3"number""string""object""object""undefined""object""number"1

Monday, 21 December 2009

Page 14: Obvious Secrets of JavaScript

Object Properties

Monday, 21 December 2009

Page 15: Obvious Secrets of JavaScript

for in

Monday, 21 December 2009

Page 16: Obvious Secrets of JavaScript

var a = { x: 12, y: 23, r: 10, draw: function () {/*...*/}};

for (var property in a) { alert("a." + property + " = " + a[property]);}

Monday, 21 December 2009

Page 17: Obvious Secrets of JavaScript

var a = { x: 12, y: 23, r: 10, draw: function () {/*...*/}};

for (var property in a) { alert("a." + property + " = " + a[property]);}

Monday, 21 December 2009

Page 18: Obvious Secrets of JavaScript

ReadOnly

DontDelete

DontEnum

Internal

Monday, 21 December 2009

Page 19: Obvious Secrets of JavaScript

Function

Monday, 21 December 2009

Page 20: Obvious Secrets of JavaScript

var y = 1;function x() { var y = 2; return ++y;}

var z = function () { return ++y;};

Monday, 21 December 2009

Page 21: Obvious Secrets of JavaScript

function x() { var y = 2; return function () { return ++y; };}

var a = x();a();a();

Monday, 21 December 2009

Page 22: Obvious Secrets of JavaScript

typeof function (){} == "function"

Monday, 21 December 2009

Page 23: Obvious Secrets of JavaScript

Arguments

Monday, 21 December 2009

Page 24: Obvious Secrets of JavaScript

function add(a, b) { return a + b;}

add(4, 5); // = 9add(4, 5, 6, 7, 8, 9) // = 39

function add() { var sum = 0; for (var i = 0, ii = arguments.length; i < ii; i++) { sum +=+ arguments[i]; } return sum;}

Monday, 21 December 2009

Page 25: Obvious Secrets of JavaScript

Scope, Context & “this”

Monday, 21 December 2009

Page 26: Obvious Secrets of JavaScript

function is(it) { alert(this + " is " + it);}

is("global");

is.call(5, "number");

is.apply("A", ["string"]);

alert.is = is;

alert.is("function");

Monday, 21 December 2009

Page 27: Obvious Secrets of JavaScript

Variable declaration

Monday, 21 December 2009

Page 28: Obvious Secrets of JavaScript

1 alert(b);

b = 1;

alert(a);

var a = 1;

(function () {

var x = 1;

})();

alert(x);

(function () {

y = 1;

})();

alert(y);

2

3

4

Monday, 21 December 2009

Page 29: Obvious Secrets of JavaScript

Function declaration

Monday, 21 December 2009

Page 30: Obvious Secrets of JavaScript

1

2

3

4

5

function x(a) {

return a && x(--a);

}

var x = function (a) {

return a && x(--a);

};

setTimeout(function (a) {

return a && arguments.callee(--a);

}, 1000);

var x = function y(a) {

return a && y(--a);

};

setTimeout(function y(a) {

return a && y(--a);

}, 1000);

Monday, 21 December 2009

Page 31: Obvious Secrets of JavaScript

Array declaration

Monday, 21 December 2009

Page 32: Obvious Secrets of JavaScript

var a = new Array;

var a = new Array(3);

var a = [];

var a = [undefined, undefined, undefined];

var a = [1, 2, 3, 4];

Monday, 21 December 2009

Page 33: Obvious Secrets of JavaScript

Object declaration (JSON)

Monday, 21 December 2009

Page 34: Obvious Secrets of JavaScript

var a = new Object;

var a = {};

var a = {x: 10, y: 15};

var a = { x: 10, name: "object", "font-style": "italic", getHeight: function () {/*...*/}, points: [1, 2, 3], child: {x: 10, y: 15}};

Monday, 21 December 2009

Page 35: Obvious Secrets of JavaScript

OOP

Monday, 21 December 2009

Page 36: Obvious Secrets of JavaScript

Object Owns Prototype

Monday, 21 December 2009

Page 37: Obvious Secrets of JavaScript

1

2

3

4

var mouse = {

name: "Mike",

voice: function () { alert("Squik!"); }

};

var o = new Object;

o.name = "Mike";

o.voice = function () { alert("Squik!"); };

var O = function () {

this.name = "Mike";

this.voice = function () { alert("Squik!"); };

};

var o = new O;

var O = function () {};

O.prototype.name = "Mike";

O.prototype.voice = function () { alert("Squik!"); };

var o = new O;

Monday, 21 December 2009

Page 38: Obvious Secrets of JavaScript

Inheritance

Monday, 21 December 2009

Page 39: Obvious Secrets of JavaScript

Delegation

Monday, 21 December 2009

Page 40: Obvious Secrets of JavaScript

Class

Object Object Object

Class

Object

Classic Model

Monday, 21 December 2009

Page 41: Obvious Secrets of JavaScript

Object Object Object

Object

Prototypal Model

Monday, 21 December 2009

Page 42: Obvious Secrets of JavaScript

var A = function () {};

A1

Monday, 21 December 2009

Page 43: Obvious Secrets of JavaScript

var A = function () {};A.prototype.x = 5;

A1

x: 5

Monday, 21 December 2009

Page 44: Obvious Secrets of JavaScript

var A = function () {};A.prototype.x = 5;

var b = new A;

b

A1

x: 5

Monday, 21 December 2009

Page 45: Obvious Secrets of JavaScript

var A = function () {};A.prototype.x = 5;

var b = new A;A.prototype.y = 6;

b

A1

x: 5y: 6

Monday, 21 December 2009

Page 46: Obvious Secrets of JavaScript

var A = function () {};A.prototype.x = 5;

var b = new A;A.prototype.y = 6;var c = new A;

b

A1

c

x: 5y: 6

Monday, 21 December 2009

Page 47: Obvious Secrets of JavaScript

var A = function () {};A.prototype.x = 5;

var b = new A;A.prototype.y = 6;var c = new A;b.z = 7;

b

A1

c

x: 5y: 6

z: 7

Monday, 21 December 2009

Page 48: Obvious Secrets of JavaScript

var A = function () {};A.prototype.x = 5;

var b = new A;A.prototype.y = 6;var c = new A;b.z = 7;c.x = 4;

b

A1

c

x: 5y: 6

z: 7 x: 4

Monday, 21 December 2009

Page 49: Obvious Secrets of JavaScript

var A = function () {};A.prototype.x = 5;

var b = new A;A.prototype.y = 6;var c = new A;b.z = 7;c.x = 4;

A.prototype = { w: 1, u: 2};

b

A1

c

2

x: 5y: 6

w: 1u: 2

z: 7 x: 4

Monday, 21 December 2009

Page 50: Obvious Secrets of JavaScript

var A = function () {};A.prototype.x = 5;

var b = new A;A.prototype.y = 6;var c = new A;b.z = 7;c.x = 4;

A.prototype = { w: 1, u: 2};

var d = new A;

b

A1

c

2

d

x: 5y: 6

w: 1u: 2

z: 7 x: 4 —

*

Monday, 21 December 2009

Page 51: Obvious Secrets of JavaScript

var A = function () {};A.prototype.x = 5;

var b = new A;A.prototype.y = 6;var c = new A;b.z = 7;c.x = 4;

A.prototype = { w: 1, u: 2};

var d = new A;

b

A1

c

2

d

x: 5y: 6

w: 1u: 2

z: 7 x: 4 —

*

Monday, 21 December 2009

Page 52: Obvious Secrets of JavaScript

Thank You

Monday, 21 December 2009