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

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.

主站蜘蛛池模板: 论坛| 阿合奇县| 乌拉特前旗| 游戏| 楚雄市| 梅州市| 澎湖县| 左贡县| 博白县| 阿克陶县| 巴东县| 南汇区| 岚皋县| 平和县| 堆龙德庆县| 四川省| 武宁县| 昌吉市| 定陶县| 竹北市| 海伦市| 东兰县| 哈巴河县| 平泉县| 镇江市| 招远市| 巧家县| 日喀则市| 唐山市| 平定县| 庆元县| 淄博市| 漳浦县| 股票| 双江| 商洛市| 镇坪县| 嵊州市| 临江市| 蓝山县| 济阳县|