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

Classes

A class is an extensible template that is used to create objects with member variables to hold the state of the object and member functions that deal with the behavior of the object.

JavaScript only supports function-based and prototype-based inheritance to build reusable components. ECMAScript 6 provides the syntactic sugar of using classes in supporting object-oriented programming. However, not all browsers understand ES6 and we need transpilers, such as TypeScript, that compile the code down to JavaScript and target ES5, which is compatible with all browsers and platforms:

class Customer { 
    name: string; 
    constructor(name: string) { 
        this.name = name; 
   } 
    logCustomer() { 
        console.log('customer name is ' + this.name; 
   } 
} 
  
var customer = new Customer("Rajesh Gunasundaram"); 

This Customer class has three members: a name property, a constructor, and a logCustomer method. The last statement outside the customer class creates an instance of the customer class using the new keyword.

主站蜘蛛池模板: 昌都县| 罗田县| 隆尧县| 清镇市| 富宁县| 静海县| 会泽县| 普格县| 崇州市| 蒙山县| 建始县| 泰宁县| 庄河市| 桐柏县| 武邑县| 景德镇市| 同江市| 都安| 岗巴县| 福安市| 包头市| 乐昌市| 文水县| 腾冲县| 南通市| 林周县| 顺义区| 桃源县| 襄城县| 鹤庆县| 锡林浩特市| 大荔县| 普洱| 青冈县| 桐乡市| 余江县| 连州市| 措勤县| 宾阳县| 普宁市| 龙岩市|