O compilador do TypeScript gera um código JS e pode ser visto como ele implementa. Pode fazer pela web.
class Person {
protected name: string;
constructor(name: string) { this.name = name; }
}
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");
console.log(howard.getElevatorPitch());
console.log(howard.name); // error
abstract class Department {
constructor(public name: string) {
}
printName(): void {
console.log("Department name: " + this.name);
}
abstract printMeeting(): void; // must be implemented in derived classes
}
class AccountingDepartment extends Department {
constructor() {
super("Accounting and Auditing"); // constructors in derived classes must call super()
}
printMeeting(): void {
console.log("The Accounting Department meets each Monday at 10am.");
}
generateReports(): void {
console.log("Generating accounting reports...");
}
}
let department: Department; // ok to create a reference to an abstract type
department = new Department(); // error: cannot create an instance of an abstract class
department = new AccountingDepartment(); // ok to create and assign a non-abstract subclass
department.printName();
department.printMeeting();
department.generateReports(); // error: method doesn't exist on declared abstract type
Coloquei no GitHub para referência futura.
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var Person = /** @class */ (function () {
function Person(name) {
this.name = name;
}
return Person;
}());
var Employee = /** @class */ (function (_super) {
__extends(Employee, _super);
function Employee(name, department) {
var _this = _super.call(this, name) || this;
_this.department = department;
return _this;
}
Employee.prototype.getElevatorPitch = function () {
return "Hello, my name is " + this.name + " and I work in " + this.department + ".";
};
return Employee;
}(Person));
var howard = new Employee("Howard", "Sales");
console.log(howard.getElevatorPitch());
console.log(howard.name); // error
var Department = /** @class */ (function () {
function Department(name) {
this.name = name;
}
Department.prototype.printName = function () {
console.log("Department name: " + this.name);
};
return Department;
}());
var AccountingDepartment = /** @class */ (function (_super) {
__extends(AccountingDepartment, _super);
function AccountingDepartment() {
return _super.call(this, "Accounting and Auditing") || this;
}
AccountingDepartment.prototype.printMeeting = function () {
console.log("The Accounting Department meets each Monday at 10am.");
};
AccountingDepartment.prototype.generateReports = function () {
console.log("Generating accounting reports...");
};
return AccountingDepartment;
}(Department));
var department; // ok to create a reference to an abstract type
department = new Department(); // error: cannot create an instance of an abstract class
department = new AccountingDepartment(); // ok to create and assign a non-abstract subclass
department.printName();
department.printMeeting();
department.generateReports(); // error: method doesn't exist on declared abstract type
Então o membro protegido na verdade é privado, e é mesmo, só que na extensão ele dá um acesso especial à classe base.
Pode estar se perguntando como garante que não é acessado algo sem permissão. Simples, o compilador não aceita código que faz isto. E aí deve perguntar se pode modificar o JS de Employee
e acessar o que não deve. Sim, pode, é como estivesse acessando por reflexão, está subvertendo as garantias da linguagens acessando o código gerado. O acesso fora da classe não pode ser feito porque o JS oferece essa garantia
O método abstrato simplesmente não é gerado, como ele é um contrato só o compilador TS precisa saber que ele existe.