xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Typescript & classes & public shorthand

classes & public shorthand

Also of note, the use of public on arguments to the constructor is a shorthand that allows us to automatically create properties with that name.

http://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html#classes

with public


class Student {
    fullName: string;
    constructor(public firstName: string, public middleInitial: string, public lastName: string) {
        this.fullName = firstName + " " + middleInitial + " " + lastName;
    }
}
interface Person {
    firstName: string;
    lastName: string;
}
function hello(person: Person) {
    // return "Hello, " + person.firstName + " " + person.lastName;
    let {
        firstName,
        lastName,
    } = person;
    return `Hello, ${firstName} ${lastName}`;
}

let student = new Student("xgqfrms", "X.", "webgeeker");
let log = console.log;
log(student.firstName);
log(student.middleInitial);
log(student.lastName);
log(hello(student));
// document.body.innerHTML = hello(student);

no public


class Student {
    fullName: string;
    firstName: string;
    middleInitial: string;
    lastName: string;
    /// no public
    constructor(firstName: string, middleInitial: string, lastName: string) {
        this.firstName = firstName;
        this.middleInitial = middleInitial;
        this.lastName = lastName;
        this.fullName = firstName + " " + middleInitial + " " + lastName;
    }
}
interface Person {
    firstName: string;
    lastName: string;
}
function hello(person: Person) {
    // return "Hello, " + person.firstName + " " + person.lastName;
    let {
        firstName,
        lastName,
    } = person;
    return `Hello, ${firstName} ${lastName}`;
}

let student = new Student("xgqfrms", "X.", "webgeeker");
let log = console.log;
log(student.firstName);
log(student.middleInitial);
log(student.lastName);
log(hello(student));
// document.body.innerHTML = hello(student);


posted @ 2019-06-06 11:30  xgqfrms  阅读(199)  评论(2编辑  收藏  举报