From 02649793fb124e99670709d5dd6a8e3167d22c4a Mon Sep 17 00:00:00 2001 From: AbubakerB Date: Thu, 18 Feb 2016 21:54:17 +0000 Subject: [PATCH 1/3] Add private and protected constructor topic --- pages/Classes.md | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/pages/Classes.md b/pages/Classes.md index ddc5e4b6e..6a0378743 100644 --- a/pages/Classes.md +++ b/pages/Classes.md @@ -109,7 +109,7 @@ class Animal { ## Understanding `private` -When a member is marked `private`, it cannot be accessed from outside of its containing class. For example: +When a constructor or member is marked `private`, it cannot be accessed from outside of its containing class. For example: ```ts class Animal { @@ -117,7 +117,13 @@ class Animal { constructor(theName: string) { this.name = theName; } } +class Employee { + private name: string; + private constructor(theName: string) { this.name = theName; } +} + new Animal("Cat").name; // Error: 'name' is private; +new Employee("Bob"); // Error: 'Employee' is private; ``` TypeScript is a structural type system. @@ -189,6 +195,32 @@ console.log(howard.name); // error Notice that while we can't use `name` from outside of `Person`, we can still use it from within an instance method of `Employee` because `Employee` derives from `Person`. +A constructor may also be marked `protected`. This means that the class cannot be accessed outside of its containing class, but can be extended. For example, + +```ts +class Person { + protected name: string; + protected constructor(name: string) { this.name = name; } +} + +// Employee can extend Person +class Employee extends Person { + private department: string; + + constructor(name: string, department: string) { + super(name); + this.department = department; + } + + public getElevatorPitch() { + return `Hello, my name is ${this.name} and I work in ${this.department}.`; + } +} + +let howard = new Employee("Howard", "Sales"); +let john = new Person("John"); // Error: 'Person' is protected and only accessible within it's class. +``` + ## Parameter properties In our last example, we had to declare a private member `name` and a constructor parameter `theName`, and we then immediately set `name` to `theName`. From 0f8feff8aa5794962ad319e61bfa954fea85b592 Mon Sep 17 00:00:00 2001 From: AbubakerB Date: Fri, 19 Feb 2016 21:14:20 +0000 Subject: [PATCH 2/3] Addressed PR --- pages/Classes.md | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/pages/Classes.md b/pages/Classes.md index 6a0378743..41eb7b3f7 100644 --- a/pages/Classes.md +++ b/pages/Classes.md @@ -109,7 +109,7 @@ class Animal { ## Understanding `private` -When a constructor or member is marked `private`, it cannot be accessed from outside of its containing class. For example: +When a member is marked `private`, it cannot be accessed from outside of its containing class. For example: ```ts class Animal { @@ -117,13 +117,7 @@ class Animal { constructor(theName: string) { this.name = theName; } } -class Employee { - private name: string; - private constructor(theName: string) { this.name = theName; } -} - new Animal("Cat").name; // Error: 'name' is private; -new Employee("Bob"); // Error: 'Employee' is private; ``` TypeScript is a structural type system. @@ -195,12 +189,13 @@ console.log(howard.name); // error Notice that while we can't use `name` from outside of `Person`, we can still use it from within an instance method of `Employee` because `Employee` derives from `Person`. -A constructor may also be marked `protected`. This means that the class cannot be accessed outside of its containing class, but can be extended. For example, +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 class Person { protected name: string; - protected constructor(name: string) { this.name = name; } + protected constructor(theName: string) { this.name = theName; } } // Employee can extend Person @@ -218,12 +213,12 @@ class Employee extends Person { } let howard = new Employee("Howard", "Sales"); -let john = new Person("John"); // Error: 'Person' is protected and only accessible within it's class. +let john = new Person("John"); // Error: The 'Person' constructor is protected ``` ## Parameter properties -In our last example, we had to declare a private member `name` and a constructor parameter `theName`, and we then immediately set `name` to `theName`. +In our last example, we had to declare a protected member `name` and a constructor parameter `theName` in the `Person` class, and we then immediately set `name` to `theName`. 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 `Animal` class using a parameter property: From 05bd50c606841811938296d65e50128f87e94fb4 Mon Sep 17 00:00:00 2001 From: AbubakerB Date: Fri, 19 Feb 2016 21:16:41 +0000 Subject: [PATCH 3/3] Removed whitespace --- pages/Classes.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pages/Classes.md b/pages/Classes.md index 41eb7b3f7..ba234e222 100644 --- a/pages/Classes.md +++ b/pages/Classes.md @@ -189,7 +189,7 @@ console.log(howard.name); // error Notice that while we can't use `name` from outside of `Person`, we can still use it from within an instance method of `Employee` because `Employee` derives from `Person`. -A constructor may also be marked `protected`. +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 @@ -199,7 +199,7 @@ class Person { } // Employee can extend Person -class Employee extends Person { +class Employee extends Person { private department: string; constructor(name: string, department: string) {