我们知道,想要给Person增加新的属性,需要通过prototype才能设置。直接通过
Person.nationality = "English";
设置是无效的。但是,仍让可以访问设置的值,例如:
console.log(Person.nationality);
解释:TypeScript编译后的类的静态属性就是这样设置。与java的类变量相似。
demo:
function Person(first, last, age, eyecolor) { this.firstName = first; this.lastName = last; this.age = age; this.eyeColor = eyecolor; } var myFather = new Person("John", "Doe", 50, "blue"); var myMother = new Person("Sally", "Rally", 48, "green"); Person.nationality = "English"; console.log(Person.nationality); console.log(myFather); //Person {firstName: 'John', lastName: 'Doe', age: 50, eyeColor: 'blue'} // 虽然无法通过Person.nationality = "English";给Person类设置新的属性,但是仍可以通过Person.nationality得到设置的值。相当于TypeScript中的静态变量