Skip to content

Commit 21346c1

Browse files
dpashutskiiaddyosmani
authored andcommitted
ES5 to ES2015 (#220)
* add es2015 files, finish modernize 0 and 1 chapters * Finish 2 chapter * add 3 chapter * add 4 chapter * add chapter 5 part 1 observer * add chapter 5 part 2 publish/subscribe * add chapter 6 * add 7 chapter * add 8 chapter * add 9 chapter * add chapter 10 * add 11 chapter * add 12 chapter and small fixes * add 13 chapter * add 14 chapter * add 15, 16, 17 chapters without changes * add 18 and 21 chapters without changes, fix old code * add 19 chapter * add 20 chapter * change snippets structure * add 22, 23, 24, 28 chapters without changes * add 25 chapter * add 26 chapter * add 27 chapter * add 29 chapter * add 30 chapter * add 31 chapter * add 32 chapter * refactoring 1 section - main patterns * refactoring remaining sections * fix singleton * add new AMD syntax, fix spelling mistakes * add new module pattern implementation * add new Mixins
1 parent 88d5dfc commit 21346c1

File tree

61 files changed

+11428
-7
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+11428
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//*******************************************************//
2+
// A brief note on classes
3+
//*******************************************************//
4+
5+
// Section contains description of ES2015, but not use it.
6+
// I suggest remove the description and put the new examples.
7+
8+
//********************** Snippet 1 **********************//
9+
10+
// A car "class"
11+
12+
// [ES2015+] Below we used new class declaration, using keyword class
13+
// [ES2015+] We used new constructor method and method declaration
14+
// [ES2015+] Classes are syntactic sugar over JavaScript's prototype-based inheritance
15+
// [ES2015+] We used new template literals for string interpolation
16+
class Car {
17+
constructor(model) {
18+
this.model = model;
19+
this.color = 'silver';
20+
this.year = '2012';
21+
}
22+
23+
getInfo() {
24+
return `${this.model} ${this.year}`;
25+
}
26+
}
27+
28+
//********************** Snippet 2 **********************//
29+
30+
// Usage:
31+
32+
// [ES2015+] We used new keyword const for immutable constant declaration
33+
const myCar = new Car('ford');
34+
35+
myCar.year = '2010';
36+
37+
console.log(myCar.getInfo());
38+
39+
// Here the link on Stoyan Stefanov's post, it's a good post.
40+
// But more modern data can be obtained here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//*******************************************************//
2+
// A brief note on classes
3+
//*******************************************************//
4+
5+
//********************** Snippet 1 **********************//
6+
7+
// A car "class"
8+
function Car( model ) {
9+
10+
this.model = model;
11+
this.color = "silver";
12+
this.year = "2012";
13+
14+
this.getInfo = function () {
15+
return this.model + " " + this.year;
16+
};
17+
18+
}
19+
20+
//********************** Snippet 2 **********************//
21+
22+
var myCar = new Car("ford");
23+
24+
myCar.year = "2010";
25+
26+
console.log( myCar.getInfo() );
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
//*******************************************************//
2+
// Object Creation
3+
//*******************************************************//
4+
5+
//********************** Snippet 1 **********************//
6+
7+
// [ES2015+] We used new keyword const for immutable constant declaration
8+
// Each of the following options will create a new empty object:
9+
10+
const newObject = {};
11+
12+
// or
13+
const newObject = Object.create(Object.prototype);
14+
15+
// or
16+
const newObject = new Object();
17+
18+
//********************** Snippet 2 **********************//
19+
20+
// ECMAScript 3 compatible approaches
21+
22+
// 1. Dot syntax
23+
24+
// Set properties
25+
newObject.someKey = 'Hello World';
26+
27+
// Get properties
28+
// [ES2015+] We used new keyword const for immutable constant declaration
29+
const value = newObject.someKey;
30+
31+
// 2. Square bracket syntax
32+
33+
// Set properties
34+
newObject['Some Key'] = 'Hello World';
35+
36+
// Get properties
37+
// [ES2015+] We used new keyword const for immutable constant declaration
38+
const value = newObject['Some Key'];
39+
40+
// ECMAScript 5 only compatible approaches
41+
// For more information see: http://kangax.github.com/es5-compat-table/
42+
43+
// 3. Object.defineProperty
44+
45+
// Set properties
46+
Object.defineProperty(newObject, 'someKey', {
47+
value: "for more control of the property's behavior",
48+
writable: true,
49+
enumerable: true,
50+
configurable: true,
51+
});
52+
53+
// If the above feels a little difficult to read, a short-hand could
54+
// be written as follows:
55+
// [ES2015+] We used new keyword const for immutable constant declaration
56+
// [ES2015+] We used new arrow function expression syntax
57+
const defineProp = (obj, key, value) => {
58+
const config = {
59+
value: value,
60+
writable: true,
61+
enumerable: true,
62+
configurable: true,
63+
};
64+
Object.defineProperty(obj, key, config);
65+
};
66+
67+
// To use, we then create a new empty "person" object
68+
// [ES2015+] We used new keyword const for immutable constant declaration
69+
const person = Object.create(Object.prototype);
70+
71+
// Populate the object with properties
72+
defineProp(person, 'car', 'Delorean');
73+
defineProp(person, 'dateOfBirth', '1981');
74+
defineProp(person, 'hasBeard', false);
75+
76+
console.log(person);
77+
// Outputs: Object {car: "Delorean", dateOfBirth: "1981", hasBeard: false}
78+
79+
// 4. Object.defineProperties
80+
81+
// Set properties
82+
Object.defineProperties(newObject, {
83+
someKey: {
84+
value: 'Hello World',
85+
writable: true,
86+
},
87+
88+
anotherKey: {
89+
value: 'Foo bar',
90+
writable: false,
91+
},
92+
});
93+
94+
// Getting properties for 3. and 4. can be done using any of the
95+
// options in 1. and 2.
96+
97+
//********************** Snippet 3 **********************//
98+
99+
// Usage:
100+
101+
// Create a race car driver that inherits from the person object
102+
// [ES2015+] We used new keyword const for immutable constant declaration
103+
const driver = Object.create(person);
104+
105+
// Set some properties for the driver
106+
defineProp(driver, 'topSpeed', '100mph');
107+
108+
// Get an inherited property (1981)
109+
console.log(driver.dateOfBirth);
110+
111+
// Get the property we set (100mph)
112+
console.log(driver.topSpeed);
113+
114+
//*******************************************************//
115+
// Basic Constructors
116+
//*******************************************************//
117+
118+
//********************** Snippet 1 **********************//
119+
120+
// [ES2015+] Below we used new class declaration, using keyword class
121+
// [ES2015+] We used new constructor method and method declaration
122+
// [ES2015+] Classes are syntactic sugar over JavaScript's prototype-based inheritance
123+
// [ES2015+] We used new template literals for string interpolation
124+
class Car {
125+
constructor(model, year, miles) {
126+
this.model = model;
127+
this.year = year;
128+
this.miles = miles;
129+
}
130+
131+
toString() {
132+
return `${this.model} has done ${this.miles} miles`;
133+
}
134+
}
135+
136+
// Usage:
137+
138+
// We can create new instances of the car
139+
// [ES2015+] We used new keyword const for immutable constant declaration
140+
const civic = new Car('Honda Civic', 2009, 20000);
141+
const mondeo = new Car('Ford Mondeo', 2010, 5000);
142+
143+
// and then open our browser console to view the
144+
// output of the toString() method being called on
145+
// these objects
146+
console.log(civic.toString());
147+
console.log(mondeo.toString());
148+
149+
//*******************************************************//
150+
// Constructors With Prototypes
151+
//*******************************************************//
152+
153+
//********************** Snippet 1 **********************//
154+
// [ES2015+] Classes are syntactic sugar over JavaScript's prototype-based inheritance
155+
// [ES2015+] We used new constructor method and method declaration
156+
// [ES2015+] All of it new syntax sugar above old function structures
157+
class Car {
158+
constructor(model, year, miles) {
159+
this.model = model;
160+
this.year = year;
161+
this.miles = miles;
162+
}
163+
}
164+
165+
// Note here that we are using Object.prototype.newMethod rather than
166+
// Object.prototype so as to avoid redefining the prototype object
167+
// [ES2015+] We still could use Object.prototype for adding new methods, because internally we use the same structure
168+
// [ES2015+] We used new template literals for string interpolation
169+
Car.prototype.toString = function() {
170+
return `${this.model} has done ${this.miles} miles`;
171+
};
172+
173+
// Usage:
174+
// [ES2015+] We used new keyword const for immutable constant declaration
175+
const civic = new Car('Honda Civic', 2009, 20000);
176+
const mondeo = new Car('Ford Mondeo', 2010, 5000);
177+
178+
console.log(civic.toString());
179+
console.log(mondeo.toString());

0 commit comments

Comments
 (0)