查看“ECMAScript6: 类”的源代码
←
ECMAScript6: 类
跳转至:
导航
,
搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看与复制此页面的源代码。
ES引入了class的概念,新的class写法让对象原型的写法更加清晰 ==定义类== <nowiki> class Car{ // 等价于Car构造函数 constructor(sColor,iDoors){ this.color= sColor; this.doors= iDoors; } // 等价于Car.prototype.showColor showColor(){ console.log(this.color); } } let oCar = new Car("red",4); oCar.showColor();</nowiki> 与函数一样,类也可以使用表达式的形式定义: <nowiki> let Car = class { // 等价于Car构造函数 constructor(sColor,iDoors){ this.color= sColor; this.doors= iDoors; } // 等价于Car.prototype.showColor showColor(){ console.log(this.color); } } let oCar = new Car("red",4); oCar.showColor();</nowiki> 使用类表达式,可以立即调用类构造函数从而构建一共类的单例对象: <nowiki> let car = new class { // 等价于Car构造函数 constructor(sColor,iDoors){ this.color= sColor; this.doors= iDoors; } // 等价于Car.prototype.showColor showColor(){ console.log(this.color); } }("red", 4); car.showColor();</nowiki> ==访问器属性==
返回至
ECMAScript6: 类
。
导航菜单
个人工具
登录
命名空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
最近更改
随机页面
帮助
工具
链入页面
相关更改
特殊页面
页面信息