Skip to content

Instantly share code, notes, and snippets.

@winwu
Last active July 25, 2016 15:08
Show Gist options
  • Save winwu/09f5a22c2cec4c2f46e7be4bdbd7f5b6 to your computer and use it in GitHub Desktop.
Save winwu/09f5a22c2cec4c2f46e7be4bdbd7f5b6 to your computer and use it in GitHub Desktop.
/*
* TypeScript 可以使用 ES6 的 class 語法
* 但 TypeScript 還可以額外使用靜態型別的那種修飾字:
* 如 public, protected, private
*/
class Employee {
// name is public!
public name: string;
// manager is private, cannot be access from outside
private manager: string;
public constructor(theName: string, theManager: string) {
this.name = theName;
this.manager = theManager;
}
}
// get "Win Wu"
alert(new Employee("Win Wu", "Max").name);
/* get error:
* Property 'manager' is
* private and only accessible within class 'Employee'.
*/
// alert(new Employee("Win Wu", "Max").manager);
@winwu
Copy link
Author

winwu commented Jul 25, 2016

/*
 * TypeScript 的 class 也有靜態型別的那種修飾字:
 * public, protected, private
 */
var Employee = (function () {
    function Employee(theName, theManager) {
        this.name = theName;
        this.manager = theManager;
    }
    return Employee;
}());
// get "Win Wu"
alert(new Employee("Win Wu", "Max").name);
// get error hint!
alert(new Employee("Win Wu", "Max").manager);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment