Addition of ignore notation
Some snippets are designed to show syntax errors or refer to earlier variables. In this case the automated checker needs to avoid compiling them
This commit is contained in:
parent
41a4dfeb68
commit
807e581c3d
@ -43,7 +43,7 @@ Of course, one of the most fundamental patterns in class-based programming is be
|
||||
|
||||
Let's take a look at an example:
|
||||
|
||||
```ts
|
||||
```ts-ignore
|
||||
class Animal {
|
||||
name: string;
|
||||
constructor(theName: string) { this.name = theName; }
|
||||
@ -105,7 +105,7 @@ In TypeScript, each member is `public` by default.
|
||||
You may still mark a member `public` explicitly.
|
||||
We could have written the `Animal` class from the previous section in the following way:
|
||||
|
||||
```ts
|
||||
```ts-ignore
|
||||
class Animal {
|
||||
public name: string;
|
||||
public constructor(theName: string) { this.name = theName; }
|
||||
@ -119,7 +119,7 @@ class Animal {
|
||||
|
||||
When a member is marked `private`, it cannot be accessed from outside of its containing class. For example:
|
||||
|
||||
```ts
|
||||
```ts-ignore
|
||||
class Animal {
|
||||
private name: string;
|
||||
constructor(theName: string) { this.name = theName; }
|
||||
@ -138,7 +138,7 @@ The same applies to `protected` members.
|
||||
|
||||
Let's look at an example to better see how this plays out in practice:
|
||||
|
||||
```ts
|
||||
```ts-ignore
|
||||
class Animal {
|
||||
private name: string;
|
||||
constructor(theName: string) { this.name = theName; }
|
||||
@ -174,7 +174,7 @@ Even though `Employee` also has a `private` member called `name`, it's not the o
|
||||
The `protected` modifier acts much like the `private` modifier with the exception that members
|
||||
declared `protected` can also be accessed by instances of deriving classes. For example,
|
||||
|
||||
```ts
|
||||
```ts-ignore
|
||||
class Person {
|
||||
protected name: string;
|
||||
constructor(name: string) { this.name = name; }
|
||||
@ -204,7 +204,7 @@ we can still use it from within an instance method of `Employee` because `Employ
|
||||
A constructor may also be marked `protected`.
|
||||
This means that the class cannot be instantiated outside of its containing class, but can be extended. For example,
|
||||
|
||||
```ts
|
||||
```ts-ignore
|
||||
class Person {
|
||||
protected name: string;
|
||||
protected constructor(theName: string) { this.name = theName; }
|
||||
@ -233,7 +233,7 @@ let john = new Person("John"); // Error: The 'Person' constructor is protected
|
||||
You can make properties readonly by using the `readonly` keyword.
|
||||
Readonly properties must be initialized at their declaration or in the constructor.
|
||||
|
||||
```ts
|
||||
```ts-ignore
|
||||
class Octopus {
|
||||
readonly name: string;
|
||||
readonly numberOfLegs: number = 8;
|
||||
@ -252,7 +252,7 @@ This turns out to be a very common practice.
|
||||
*Parameter properties* let you create and initialize a member in one place.
|
||||
Here's a further revision of the previous `Octopus` class using a parameter property:
|
||||
|
||||
```ts
|
||||
```ts-ignore
|
||||
class Octopus {
|
||||
readonly numberOfLegs: number = 8;
|
||||
constructor(readonly name: string) {
|
||||
|
@ -45,7 +45,7 @@ TypeScript can figure the return type out by looking at the return statements, s
|
||||
|
||||
In TypeScript, the number of arguments given to a function has to match the number of parameters the function expects.
|
||||
|
||||
```ts
|
||||
```ts-ignore
|
||||
function buildName(firstName: string, lastName: string) {
|
||||
return firstName + " " + lastName;
|
||||
}
|
||||
@ -60,7 +60,7 @@ When they do, their value is `undefined`.
|
||||
We can get this functionality in TypeScript by adding a `?` to the end of parameters we want to be optional.
|
||||
For example, let's say we want the last name parameter from above to be optional:
|
||||
|
||||
```ts
|
||||
```ts-ignore
|
||||
function buildName(firstName: string, lastName?: string) {
|
||||
if (lastName)
|
||||
return firstName + " " + lastName;
|
||||
@ -80,7 +80,7 @@ In TypeScript, we can also set a value that a parameter will be assigned if the
|
||||
These are called default-initialized parameters.
|
||||
Let's take the previous example and default the last name to `"Smith"`.
|
||||
|
||||
```ts
|
||||
```ts-ignore
|
||||
function buildName(firstName: string, lastName = "Smith") {
|
||||
return firstName + " " + lastName;
|
||||
}
|
||||
@ -115,7 +115,7 @@ Unlike plain optional parameters, default-initialized parameters don't *need* to
|
||||
If a default-initialized parameter comes before a required parameter, users need to explicitly pass `undefined` to get the default initialized value.
|
||||
For example, we could write our last example with only a default initializer on `firstName`:
|
||||
|
||||
```ts
|
||||
```ts-ignore
|
||||
function buildName(firstName = "Will", lastName: string) {
|
||||
return firstName + " " + lastName;
|
||||
}
|
||||
@ -134,7 +134,7 @@ In JavaScript, you can work with the arguments directly using the `arguments` va
|
||||
|
||||
In TypeScript, you can gather these arguments together into a variable:
|
||||
|
||||
```ts
|
||||
```ts-ignore
|
||||
function buildName(firstName: string, ...restOfName: string[]) {
|
||||
return firstName + " " + restOfName.join(" ");
|
||||
}
|
||||
@ -148,7 +148,7 @@ The compiler will build an array of the arguments passed in with the name given
|
||||
|
||||
The ellipsis is also used in the type of the function with rest parameters:
|
||||
|
||||
```ts
|
||||
```ts-ignore
|
||||
function buildName(firstName: string, ...restOfName: string[]) {
|
||||
return firstName + " " + restOfName.join(" ");
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ Another property of block-scoped variables is that they can't be read or written
|
||||
While these variables are "present" throughout their scope, all points up until their declaration are part of their *temporal dead zone*.
|
||||
This is just a sophisticated way of saying you can't access them before the `let` statement, and luckily TypeScript will let you know that.
|
||||
|
||||
```typescript
|
||||
```typescript-ignore
|
||||
a++; // illegal to use 'a' before it's declared;
|
||||
let a;
|
||||
```
|
||||
|
@ -41,7 +41,7 @@ let img = images.createImage(`
|
||||
|
||||
### ~
|
||||
|
||||
```blocks
|
||||
```typescript-ignore
|
||||
let state = img.pixel(0, 0)
|
||||
```
|
||||
|
||||
|
@ -32,7 +32,7 @@ let img = images.createImage(`
|
||||
|
||||
### ~
|
||||
|
||||
```blocks
|
||||
```typescript-ignore
|
||||
let w = img.width()
|
||||
```
|
||||
|
||||
@ -40,7 +40,7 @@ let w = img.width()
|
||||
|
||||
The following example uses the `width` function with a [for](/blocks/loops/for) loop to show each image frame on the screen:
|
||||
|
||||
```blocks
|
||||
```typescript
|
||||
let img2 = images.createImage(`
|
||||
. . # . . . # # # # . # # # .
|
||||
. # # . . . . . . # . . . # .
|
||||
|
Loading…
Reference in New Issue
Block a user