官术网_书友最值得收藏!

Classes

I am a guy who believes that JavaScript is an object-based programming language and not an object-oriented programming language, and I know quite a lot of people who disagree with me.

In vanilla JavaScript, we have functions, which act like a class and exhibit prototype-based inheritance. In TypeScript/ES6, we have the class construct:

class Person { 
name: string;

constructor(personName: string) {
this.name = personName;
}

getName {
return "The Name: " + this.greeting;
}
}
// somewhere else
arvind:Person = new Person('Arvind');

In the preceding example, we have defined a class named Person and we are defining the class constructor, which accepts the name on initialization of the class.

To initialize the class, we will invoke the class with a new keyword and pass in the name to the constructor. The variable that stores the instance of the class -- the object, arvind in the preceding example, can also be typed to the class. This helps in better understanding the possibilities of the arvind object.

Note: The classes in ES6 still follow Prototypal-based Inheritance and not the classical Inheritance model.

主站蜘蛛池模板: 西吉县| 瑞昌市| 灵璧县| 临西县| 老河口市| 庄河市| 灵宝市| 兰坪| 南通市| 沅陵县| 漳平市| 神木县| 富平县| 金秀| 虞城县| 和政县| 新民市| 乃东县| 浦东新区| 鲁山县| 英山县| 平罗县| 乌审旗| 洞头县| 淄博市| 法库县| 博客| 黎川县| 宁强县| 江山市| 丘北县| 青阳县| 三门峡市| 康平县| 旺苍县| 来安县| 丽水市| 瓦房店市| 西乡县| 蒙阴县| 喀什市|