Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Apress
committed
Oct 18, 2016
0 parents
commit 42ddbd9
Showing
3,522 changed files
with
610,670 additions
and
0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
var Listing1_1; | ||
(function (Listing1_1) { | ||
// Not using with | ||
var radius = 4; | ||
var area = Math.PI * radius * radius; | ||
|
||
// Using with | ||
var radius = 4; | ||
})(Listing1_1 || (Listing1_1 = {})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Listing1_1 { | ||
|
||
// Not using with | ||
var radius = 4; | ||
var area = Math.PI * radius * radius; | ||
|
||
// Using with | ||
var radius = 4; | ||
|
||
// Warning: All symbols within a with block will be resolved to 'any'. | ||
//with (Math) { | ||
// var area = PI * radius * radius; | ||
//} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
var Listing1_2; | ||
(function (Listing1_2) { | ||
var radius = 4; | ||
var area = Math.PI * radius * radius; | ||
})(Listing1_2 || (Listing1_2 = {})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module Listing1_2 { | ||
|
||
var radius = 4; | ||
var area = Math.PI * radius * radius; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
var Listing1_3; | ||
(function (Listing1_3) { | ||
function addNumbers(a, b) { | ||
// missing var keyword | ||
//total = a + b; | ||
//return total; | ||
} | ||
})(Listing1_3 || (Listing1_3 = {})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module Listing1_3 { | ||
|
||
function addNumbers(a, b) { | ||
// missing var keyword | ||
//total = a + b; | ||
//return total; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
var Listing1_10; | ||
(function (Listing1_10) { | ||
var DiscFlags; | ||
(function (DiscFlags) { | ||
DiscFlags[DiscFlags["None"] = 0] = "None"; | ||
DiscFlags[DiscFlags["Drive"] = 1] = "Drive"; | ||
DiscFlags[DiscFlags["Influence"] = 2] = "Influence"; | ||
DiscFlags[DiscFlags["Steadiness"] = 4] = "Steadiness"; | ||
DiscFlags[DiscFlags["Conscientiousness"] = 8] = "Conscientiousness"; | ||
})(DiscFlags || (DiscFlags = {})); | ||
|
||
// Using flags | ||
var personality = 1 /* Drive */ | 8 /* Conscientiousness */; | ||
|
||
// Testing flags | ||
// true | ||
var hasD = (personality & 1 /* Drive */) == 1 /* Drive */; | ||
|
||
// true | ||
var hasI = (personality & 2 /* Influence */) == 2 /* Influence */; | ||
|
||
// false | ||
var hasS = (personality & 4 /* Steadiness */) == 4 /* Steadiness */; | ||
|
||
// false | ||
var hasC = (personality & 8 /* Conscientiousness */) == 8 /* Conscientiousness */; | ||
})(Listing1_10 || (Listing1_10 = {})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module Listing1_10 { | ||
|
||
enum DiscFlags { | ||
None = 0, | ||
Drive = 1, | ||
Influence = 2, | ||
Steadiness = 4, | ||
Conscientiousness = 8 | ||
} | ||
|
||
// Using flags | ||
var personality = DiscFlags.Drive | DiscFlags.Conscientiousness; | ||
|
||
// Testing flags | ||
|
||
// true | ||
var hasD = (personality & DiscFlags.Drive) == DiscFlags.Drive; | ||
|
||
// true | ||
var hasI = (personality & DiscFlags.Influence) == DiscFlags.Influence; | ||
|
||
// false | ||
var hasS = (personality & DiscFlags.Steadiness) == DiscFlags.Steadiness; | ||
|
||
// false | ||
var hasC = (personality & DiscFlags.Conscientiousness) == DiscFlags.Conscientiousness; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
var Listing1_11; | ||
(function (Listing1_11) { | ||
var avenueRoad = { | ||
bedrooms: 11, | ||
bathrooms: 10, | ||
butlers: 1 | ||
}; | ||
|
||
// Errors: Cannot convert House to Mansion | ||
//var mansion: Mansion = avenueRoad; | ||
// Works | ||
var mansion = avenueRoad; | ||
})(Listing1_11 || (Listing1_11 = {})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module Listing1_11 { | ||
|
||
interface House { | ||
bedrooms: number; | ||
bathrooms: number; | ||
} | ||
|
||
interface Mansion { | ||
bedrooms: number; | ||
bathrooms: number; | ||
butlers: number; | ||
} | ||
|
||
var avenueRoad: House = { | ||
bedrooms: 11, | ||
bathrooms: 10, | ||
butlers: 1 | ||
}; | ||
|
||
// Errors: Cannot convert House to Mansion | ||
//var mansion: Mansion = avenueRoad; | ||
|
||
// Works | ||
var mansion: Mansion = <Mansion>avenueRoad; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
var Listing1_12; | ||
(function (Listing1_12) { | ||
var name = 'Avenue Road'; | ||
|
||
// Error: Cannot convert 'string' to 'number' | ||
//var bedrooms: number = <number> name; | ||
// Works | ||
var bedrooms = name; | ||
})(Listing1_12 || (Listing1_12 = {})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Listing1_12 { | ||
|
||
var name: string = 'Avenue Road'; | ||
|
||
// Error: Cannot convert 'string' to 'number' | ||
//var bedrooms: number = <number> name; | ||
|
||
// Works | ||
var bedrooms: number = <number> <any> name; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
var Listing1_4; | ||
(function (Listing1_4) { | ||
var name = 'Steve'; | ||
})(Listing1_4 || (Listing1_4 = {})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module Listing1_4 { | ||
|
||
var name: string = 'Steve'; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
var Listing1_5; | ||
(function (Listing1_5) { | ||
// primitive type annotation | ||
var name = 'Steve'; | ||
var heightInCentimeters = 182.88; | ||
var isActive = true; | ||
|
||
// array type annotation | ||
var names = ['James', 'Nick', 'Rebecca', 'Lily']; | ||
|
||
// function annotation with parameter type annotation and return type annotation | ||
var sayHello; | ||
|
||
// implementation of sayHello function | ||
sayHello = function (name) { | ||
return 'Hello ' + name; | ||
}; | ||
|
||
// object type annotation | ||
var person; | ||
|
||
// Implementation of a person object | ||
person = { | ||
name: 'Mark', | ||
heightInCentimeters: 183 | ||
}; | ||
})(Listing1_5 || (Listing1_5 = {})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module Listing1_5 { | ||
|
||
// primitive type annotation | ||
var name: string = 'Steve'; | ||
var heightInCentimeters: number = 182.88; | ||
var isActive: boolean = true; | ||
|
||
// array type annotation | ||
var names: string[] = ['James', 'Nick', 'Rebecca', 'Lily']; | ||
|
||
// function annotation with parameter type annotation and return type annotation | ||
var sayHello: (name: string) => string; | ||
|
||
|
||
// implementation of sayHello function | ||
sayHello = function (name: string) { | ||
return 'Hello ' + name; | ||
}; | ||
|
||
// object type annotation | ||
var person: { name: string; heightInCentimeters: number; }; | ||
|
||
// Implementation of a person object | ||
person = { | ||
name: 'Mark', | ||
heightInCentimeters: 183 | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
var Listing1_6; | ||
(function (Listing1_6) { | ||
var person = { | ||
name: 'Mark', | ||
heightInCentimeters: 183 | ||
}; | ||
})(Listing1_6 || (Listing1_6 = {})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module Listing1_6 { | ||
|
||
interface Person { | ||
name: string; | ||
heightInCentimeters: number; | ||
} | ||
|
||
var person: Person = { | ||
name: 'Mark', | ||
heightInCentimeters: 183 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
var Listing1_7; | ||
(function (Listing1_7) { | ||
// The array is typed using the Monument interface | ||
var monuments = []; | ||
|
||
// Each item added to the array is checked for type compatibility | ||
monuments.push({ | ||
name: 'Statue of Liberty', | ||
heightInMeters: 46, | ||
location: 'USA' | ||
}); | ||
|
||
monuments.push({ | ||
name: 'Peter the Great', | ||
heightInMeters: 96 | ||
}); | ||
|
||
monuments.push({ | ||
name: 'Angel of the North', | ||
heightInMeters: 20 | ||
}); | ||
|
||
function compareMonumentHeights(a, b) { | ||
if (a.heightInMeters > b.heightInMeters) { | ||
return -1; | ||
} | ||
if (a.heightInMeters < b.heightInMeters) { | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
|
||
// The array.sort method expects a comparer that accepts two Monuments | ||
var monumentsOrderedByHeight = monuments.sort(compareMonumentHeights); | ||
|
||
// Get the first element from the array, which is the tallest | ||
var tallestMonument = monumentsOrderedByHeight[0]; | ||
|
||
console.log(tallestMonument.name); // Peter the Great | ||
})(Listing1_7 || (Listing1_7 = {})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
module Listing1_7 { | ||
|
||
interface Monument { | ||
name: string; | ||
heightInMeters: number; | ||
} | ||
|
||
// The array is typed using the Monument interface | ||
var monuments: Monument[] = []; | ||
|
||
// Each item added to the array is checked for type compatibility | ||
monuments.push({ | ||
name: 'Statue of Liberty', | ||
heightInMeters: 46, | ||
location: 'USA' | ||
}); | ||
|
||
monuments.push({ | ||
name: 'Peter the Great', | ||
heightInMeters: 96 | ||
}); | ||
|
||
monuments.push({ | ||
name: 'Angel of the North', | ||
heightInMeters: 20 | ||
}); | ||
|
||
function compareMonumentHeights(a: Monument, b: Monument) { | ||
if (a.heightInMeters > b.heightInMeters) { | ||
return -1; | ||
} | ||
if (a.heightInMeters < b.heightInMeters) { | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
|
||
// The array.sort method expects a comparer that accepts two Monuments | ||
var monumentsOrderedByHeight = monuments.sort(compareMonumentHeights); | ||
|
||
// Get the first element from the array, which is the tallest | ||
var tallestMonument = monumentsOrderedByHeight[0]; | ||
|
||
console.log(tallestMonument.name); // Peter the Great | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
var Listing1_8; | ||
(function (Listing1_8) { | ||
var VehicleType; | ||
(function (VehicleType) { | ||
VehicleType[VehicleType["PedalCycle"] = 0] = "PedalCycle"; | ||
VehicleType[VehicleType["MotorCycle"] = 1] = "MotorCycle"; | ||
VehicleType[VehicleType["Car"] = 2] = "Car"; | ||
VehicleType[VehicleType["Van"] = 3] = "Van"; | ||
VehicleType[VehicleType["Bus"] = 4] = "Bus"; | ||
VehicleType[VehicleType["Lorry"] = 5] = "Lorry"; | ||
})(VehicleType || (VehicleType = {})); | ||
|
||
var type = 5 /* Lorry */; | ||
|
||
var typeName = VehicleType[type]; | ||
})(Listing1_8 || (Listing1_8 = {})); |
Oops, something went wrong.