js类的写法 发表于 2017-10-18 | js类的写法ES5标准: 写法一: 1234567891011121314151617181920212223//定义var AsyncMessage = function (orgid, orgddkey, msgtype){ var _this = this; _this.orgid = orgid; _this.orgddkey = orgddkey; _this.msgtype = msgtype || 'oa'; _this.access_token = ''; return { getDingAccessToken : function (){ console.log('我是getDingAccessToken'); }, sendAsyncMsg : function (){ console.log('我是sendAsyncMsg'); }, sendDingMsg : function () { console.log('我是sendDingMsg'); } }}//调用var mainMessage = new AsyncMessage('abcde','C7wDa_u3qa','ding');mainMessage.sendDingMsg(); 写法二: 1234567891011//通过构造函数function Point(x, y) { this.x = x; this.y = y;}Point.prototype.toString = function () { return '(' + this.x + ', ' + this.y + ')';};var p = new Point(1, 2); ES6标准 引入了 Class(类)这个概念 类和模块的内部,默认就是严格模式 1234567891011121314151617181920212223242526//定义类class Point { /*构造函数*/ constructor(x, y) { this.x = x; this.y = y; } //方法与方法之间不需要逗号 toString() { //...... } toValue() { //..... }}// 等同于Point.prototype = { constructor() {}, toString() {}, toValue() {},};