ES6——class
生活随笔
收集整理的這篇文章主要介紹了
ES6——class
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
class Point{
constructor(x,y){
this.x = x;
this.y = y;
}
toString(){
return this.x + this.y;
}
}
console.log(typeof Point);//function,說(shuō)明類的數(shù)據(jù)類型本身就是函數(shù),類本身指向構(gòu)造函數(shù)
var a = new Point(1,2);//只有x,y,并沒(méi)有constructor,__proto__中有constructor,說(shuō)明類的所有方法和屬性都定義在了類的prototype屬性上面
console.log(a)//1,2
console.log(a.toString())//3,在類的實(shí)例上調(diào)用方法,實(shí)際上是在調(diào)用原型上的方法
//類和模塊的內(nèi)部,默認(rèn)就是嚴(yán)格模式,所以不需要使用use strict指定運(yùn)行模式。只要你的代碼寫在類或模塊之中,就只有嚴(yán)格模式可用。
class Foo {
static classMethod() {
return 'hello';
}
}
Foo.classMethod() // 'hello'
var foo = new Foo();
foo.classMethod()
// TypeError: foo.classMethod is not a function
//類相當(dāng)于實(shí)例的原型,所有在類中定義的方法,都會(huì)被實(shí)例繼承。如果在一個(gè)方法前,加上static關(guān)鍵字,就表示該方法不會(huì)被實(shí)例繼承,而是直接通過(guò)類來(lái)調(diào)用,這就稱為“靜態(tài)方法”。
class Foo {
static bar () {
this.baz();
}
static baz () {
console.log('hello');
}
baz () {
console.log('world');
}
}
Foo.bar() // hello
//如果靜態(tài)方法包含this關(guān)鍵字,這個(gè)this指的是類,而不是實(shí)例。
//上面代碼中,靜態(tài)方法bar調(diào)用了this.baz,這里的this指的是Foo類,而不是Foo的實(shí)例,等同于調(diào)用Foo.baz。另外,從這個(gè)例子還可以看出,靜態(tài)方法可以與非靜態(tài)方法重名。
class A {
constructor() {
console.log(new.target.name);
}
}
class B extends A {
constructor() {
super();//子類B的構(gòu)造函數(shù)之中的super(),代表調(diào)用父類的構(gòu)造函數(shù)
}
}
//super雖然代表了父類A的構(gòu)造函數(shù),但是返回的是子類B的實(shí)例,即super內(nèi)部的this指的是B
new A() // A
new B() // B
constructor(x,y){
this.x = x;
this.y = y;
}
toString(){
return this.x + this.y;
}
}
console.log(typeof Point);//function,說(shuō)明類的數(shù)據(jù)類型本身就是函數(shù),類本身指向構(gòu)造函數(shù)
var a = new Point(1,2);//只有x,y,并沒(méi)有constructor,__proto__中有constructor,說(shuō)明類的所有方法和屬性都定義在了類的prototype屬性上面
console.log(a)//1,2
console.log(a.toString())//3,在類的實(shí)例上調(diào)用方法,實(shí)際上是在調(diào)用原型上的方法
//類和模塊的內(nèi)部,默認(rèn)就是嚴(yán)格模式,所以不需要使用use strict指定運(yùn)行模式。只要你的代碼寫在類或模塊之中,就只有嚴(yán)格模式可用。
class Foo {
static classMethod() {
return 'hello';
}
}
Foo.classMethod() // 'hello'
var foo = new Foo();
foo.classMethod()
// TypeError: foo.classMethod is not a function
//類相當(dāng)于實(shí)例的原型,所有在類中定義的方法,都會(huì)被實(shí)例繼承。如果在一個(gè)方法前,加上static關(guān)鍵字,就表示該方法不會(huì)被實(shí)例繼承,而是直接通過(guò)類來(lái)調(diào)用,這就稱為“靜態(tài)方法”。
class Foo {
static bar () {
this.baz();
}
static baz () {
console.log('hello');
}
baz () {
console.log('world');
}
}
Foo.bar() // hello
//如果靜態(tài)方法包含this關(guān)鍵字,這個(gè)this指的是類,而不是實(shí)例。
//上面代碼中,靜態(tài)方法bar調(diào)用了this.baz,這里的this指的是Foo類,而不是Foo的實(shí)例,等同于調(diào)用Foo.baz。另外,從這個(gè)例子還可以看出,靜態(tài)方法可以與非靜態(tài)方法重名。
class A {
constructor() {
console.log(new.target.name);
}
}
class B extends A {
constructor() {
super();//子類B的構(gòu)造函數(shù)之中的super(),代表調(diào)用父類的構(gòu)造函數(shù)
}
}
//super雖然代表了父類A的構(gòu)造函數(shù),但是返回的是子類B的實(shí)例,即super內(nèi)部的this指的是B
new A() // A
new B() // B
轉(zhuǎn)載于:https://www.cnblogs.com/kaw19950302/p/7834088.html
與50位技術(shù)專家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的ES6——class的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 买二手房办证件需要卡里有钱吗
- 下一篇: 标准C程序设计七---53