Obvious Secrets of JavaScript
-
Upload
dmitry-baranovskiy -
Category
Technology
-
view
3.351 -
download
5
Transcript of Obvious Secrets of JavaScript
![Page 1: Obvious Secrets of JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/1.jpg)
JavaScriptobvious secrets of
Monday, 21 December 2009
![Page 2: Obvious Secrets of JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/2.jpg)
Monday, 21 December 2009
![Page 3: Obvious Secrets of JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/3.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/4.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/5.jpg)
Types
Monday, 21 December 2009
![Page 6: Obvious Secrets of JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/6.jpg)
number
boolean
null
string
object
undefined
Monday, 21 December 2009
![Page 7: Obvious Secrets of JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/7.jpg)
typeof
undefined "undefined"
null "object"
number "number"
boolean "boolean"
string "string"
object "object"
Monday, 21 December 2009
![Page 8: Obvious Secrets of JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/8.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/9.jpg)
to String
undefined "undefined"
null "null"
number "5"
boolean "true"
string —
object .toString()
Monday, 21 December 2009
![Page 10: Obvious Secrets of JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/10.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/11.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/12.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/13.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/14.jpg)
Object Properties
Monday, 21 December 2009
![Page 15: Obvious Secrets of JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/15.jpg)
for in
Monday, 21 December 2009
![Page 16: Obvious Secrets of JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/16.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/17.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/18.jpg)
ReadOnly
DontDelete
DontEnum
Internal
Monday, 21 December 2009
![Page 19: Obvious Secrets of JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/19.jpg)
Function
Monday, 21 December 2009
![Page 20: Obvious Secrets of JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/20.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/21.jpg)
function x() { var y = 2; return function () { return ++y; };}
var a = x();a();a();
Monday, 21 December 2009
![Page 22: Obvious Secrets of JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/22.jpg)
typeof function (){} == "function"
Monday, 21 December 2009
![Page 23: Obvious Secrets of JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/23.jpg)
Arguments
Monday, 21 December 2009
![Page 24: Obvious Secrets of JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/24.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/25.jpg)
Scope, Context & “this”
Monday, 21 December 2009
![Page 26: Obvious Secrets of JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/26.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/27.jpg)
Variable declaration
Monday, 21 December 2009
![Page 28: Obvious Secrets of JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/28.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/29.jpg)
Function declaration
Monday, 21 December 2009
![Page 30: Obvious Secrets of JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/30.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/31.jpg)
Array declaration
Monday, 21 December 2009
![Page 32: Obvious Secrets of JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/32.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/33.jpg)
Object declaration (JSON)
Monday, 21 December 2009
![Page 34: Obvious Secrets of JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/34.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/35.jpg)
OOP
Monday, 21 December 2009
![Page 36: Obvious Secrets of JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/36.jpg)
Object Owns Prototype
Monday, 21 December 2009
![Page 37: Obvious Secrets of JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/37.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/38.jpg)
Inheritance
Monday, 21 December 2009
![Page 39: Obvious Secrets of JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/39.jpg)
Delegation
Monday, 21 December 2009
![Page 40: Obvious Secrets of JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/40.jpg)
Class
Object Object Object
Class
Object
Classic Model
Monday, 21 December 2009
![Page 41: Obvious Secrets of JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/41.jpg)
Object Object Object
Object
Prototypal Model
Monday, 21 December 2009
![Page 42: Obvious Secrets of JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/42.jpg)
var A = function () {};
A1
Monday, 21 December 2009
![Page 43: Obvious Secrets of JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/43.jpg)
var A = function () {};A.prototype.x = 5;
A1
x: 5
Monday, 21 December 2009
![Page 44: Obvious Secrets of JavaScript](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/44.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/45.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/46.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/47.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/48.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/49.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/50.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/51.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062312/55493ff4b4c9050a4d8b5008/html5/thumbnails/52.jpg)
Thank You
Monday, 21 December 2009