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

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.

主站蜘蛛池模板: 正蓝旗| 定结县| 华蓥市| 绥棱县| 房产| 汝南县| 平凉市| 乌拉特中旗| 九台市| 淮滨县| 北海市| 麻江县| 平利县| 常宁市| 瓦房店市| 洪江市| 定西市| 塔城市| 新泰市| 靖安县| 紫金县| 岑巩县| 仙桃市| 柘城县| 洛宁县| 泸定县| 团风县| 汝州市| 敖汉旗| 温宿县| 琼结县| 饶平县| 丹阳市| 康保县| 安岳县| 阜新市| 眉山市| 平谷区| 山丹县| 上饶市| 廉江市|