Skip to content

Commit fdb7bc5

Browse files
committed
Adding chapter 12 code
1 parent c1ca7d2 commit fdb7bc5

File tree

11 files changed

+24618
-0
lines changed

11 files changed

+24618
-0
lines changed

‎Chapter 12/traceur1.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
classBaseStructure{
2+
constructor(){
3+
console.log("Structure built");
4+
}
5+
}
6+
7+
classCastleextendsBaseStructure{
8+
constructor(name){
9+
this.name=name;
10+
super();
11+
}
12+
Build(){
13+
console.log("Castle built: "+this.name);
14+
}
15+
}

‎Chapter 12/traceur1.out.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
varBaseStructure=functionBaseStructure(){
2+
"use strict";
3+
console.log("Structure built");
4+
};
5+
($traceurRuntime.createClass)(BaseStructure,{},{});
6+
varCastle=functionCastle(name){
7+
"use strict";
8+
this.name=name;
9+
$traceurRuntime.superCall(this,$Castle.prototype,"constructor",[]);
10+
};
11+
var$Castle=Castle;
12+
($traceurRuntime.createClass)(Castle,{Build: function(){
13+
"use strict";
14+
console.log("Castle built: "+this.name);
15+
}},{},BaseStructure);

‎Chapter 12/traceur2.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
functionCreateFeast(meat,drink="wine"){
2+
console.log("The meat is: "+meat);
3+
console.log("The drink is: "+drink);
4+
}
5+
CreateFeast("Boar","Beer");
6+
CreateFeast("Venison");

‎Chapter 12/traceur2.out.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
functionCreateFeast(meat){
2+
vardrink=arguments[1]!==(void0) ? arguments[1] : "wine";
3+
console.log("The meat is: "+meat);
4+
console.log("The drink is: "+drink);
5+
}
6+
CreateFeast("Boar","Beer");
7+
CreateFeast("Venison");

‎Chapter 12/traceur3.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vararmy1Size=5000;
2+
vararmy2Size=3578;
3+
console.log(`The surviving army will be ${army1Size>army2Size ? "Army 1": "Army 2"}`);

‎Chapter 12/traceur3.out.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vararmy1Size=5000;
2+
vararmy2Size=3578;
3+
console.log(("The surviving army will be "+(army1Size>army2Size ? "Army 1" : "Army 2")));

‎Chapter 12/traceur4.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
if(true)
2+
{
3+
varoutside=9;
4+
letinside=7;
5+
}
6+
7+
console.log(outside);
8+
console.log(inside);

‎Chapter 12/traceur4.out.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
varinside$__0;
2+
if(true){
3+
varoutside=9;
4+
inside$__0=7;
5+
}
6+
console.log(outside);
7+
console.log(inside);

‎Chapter 12/traceur5.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
functiontimeout(ms){
2+
returnnewPromise((resolve)=>{
3+
setTimeout(resolve,ms);
4+
});
5+
}
6+
7+
asyncfunctionasyncValue(value){
8+
awaittimeout(1500);
9+
returnvalue;
10+
}
11+
12+
(asyncfunction(){
13+
console.log("Starting.");
14+
varvaluePromise=asyncValue(42).catch(console.error.bind(console));
15+
console.log("Task is running in the background.");
16+
console.log("Awaiting the promise.");
17+
varvalue=awaitvaluePromise;
18+
console.log("Promise resolved.");
19+
assert.equal(42,value);
20+
console.log(value);
21+
done();
22+
})();

‎Chapter 12/traceur5.out.js‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
functiontimeout(ms){
2+
returnnewPromise((function(resolve){
3+
setTimeout(resolve,ms);
4+
}));
5+
}
6+
functionasyncValue(value){
7+
return$traceurRuntime.asyncWrap(function($ctx){
8+
while(true)
9+
switch($ctx.state){
10+
case0:
11+
Promise.resolve(timeout(1500)).then($ctx.createCallback(3),$ctx.errback);
12+
return;
13+
case3:
14+
$ctx.returnValue=value;
15+
$ctx.state=5;
16+
break;
17+
case5:
18+
$ctx.state=-2;
19+
break;
20+
default:
21+
return$ctx.end();
22+
}
23+
},this);
24+
}
25+
(function(){
26+
varvaluePromise,
27+
value;
28+
return$traceurRuntime.asyncWrap(function($ctx){
29+
while(true)
30+
switch($ctx.state){
31+
case0:
32+
console.log("Starting.");
33+
valuePromise=asyncValue(42).catch(console.error.bind(console));
34+
console.log("Task is running in the background.");
35+
console.log("Awaiting the promise.");
36+
$ctx.state=5;
37+
break;
38+
case5:
39+
Promise.resolve(valuePromise).then($ctx.createCallback(2),$ctx.errback);
40+
return;
41+
case2:
42+
value=$ctx.value;
43+
$ctx.state=3;
44+
break;
45+
case3:
46+
console.log("Promise resolved.");
47+
assert.equal(42,value);
48+
console.log(value);
49+
done();
50+
$ctx.state=-2;
51+
break;
52+
default:
53+
return$ctx.end();
54+
}
55+
},this);
56+
})();

0 commit comments

Comments
(0)