1. 程式人生 > >2、Es常用語法 箭頭函式、類

2、Es常用語法 箭頭函式、類

1、箭頭函式

什麼是箭頭函式

箭頭函式的語法非常簡單,看一下最簡單的箭頭函式表示法
() => console.log('Hello')
() => {console.log('Hello')}

等同於

function(){
console.log('hello')
}

箭頭函式裡的this:

  箭頭函式裡的this指向定義時的作用域

  普通函式裡的this指向呼叫者的作用域

箭頭函式不繫結arguments

let arrowfunc = () => console.log(arguments.length)
arrowfunc()
//output arguments is not defined

如果此時我們想要獲得函式的引數可以藉助擴充套件運算子“...”

let arrowfunc = (...theArgs) => console.log(theArgs.length)
arrowfunc(1,2)
//output
2

2、類

js生成例項物件的傳統方式是通過建構函式:

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); p.toString(); //"(1,2)"

 

Es6通過class關鍵字定義類

前面不需要加function這個關鍵字,直接將函式定義放進去就行了 ,另外,方法之間不需要逗號分隔;

//定義類

class Point {
    constructor (x, y) {
        this.x =x;
        this.y =y;
      }

        toString () {
        return `( ${this
.x}, ${this.y} )`; } toValue () { return this.x+this.y; } } var p = new Point(1,2); p.toString(); //"(1,2)" p.toValue(); //3

constructor方法

constructor方法是是類的預設方法,通過new命令生成物件例項時,自動呼叫該方法,一個類必須有constructor方法,如果沒有顯示定義,一個空的constructor方法會被預設新增:

1 class Point{
2 }
3 
4 //等同於
5 class Point {
6      constructor () { }
7 }
8 //定義了一個空的類Point,JavaScript 引擎會自動為它新增一個空的constructor方法。

 

constructor方法預設返回例項物件(即this),完全可以指定返回另外一個物件;
class Person {
      constructor  () {
            return Object.create(null);
         }
}

new Person() instanceof Person
//false
//例項      instanceof 建構函式  用來判斷例項是否是建構函式的例項

 

建構函式的prototype屬性,在ES6的類上繼續存在,實際上,類的所有方法都定義在類的prototype屬性上面;

//定義類
class Point {
        constructor (x, y) {
           this.x =x;
            this.y =y;
          } 
      toString () {
          return `(${this.x},${this.y})`;
       }
}

var point = new Point(1,2);

point.toString();//(1,2)

point.hasOwnProperty("x"); //true
point.hasOwnProperty("y"); //true
point.hasOwnProperty("toString");//fasle
point.__proto__.hasOwnProperty("toString");//true

 

clss表示式

和函式一樣,類也可以使用表示式的形式定義:

const MyClass = class Me{
        getClassName () {
               return Me.name ;
            }
};


let inst  = new MyClass();
inst .getClassName();
//"Me"
Me.name
//ReferenceError :Me is not defined

 

Me只有在Class內部有定義;
如果類的內部沒有用到的話,可以省略Me,可以改寫成:

const MyClass = class {
    getClassName () {
               return  ;
            }
}

 

採用Class表示式,可以寫出立即執行Class
let person = new class {
      constructor (name) {
          this.name = name ;
          }

       sayName() {
                  console.log(this.name);
          }
}("張三");

person.sayName();
//"張三"

 

class不存在變數提升

new Foo();//ReferenceError
class Foo();

 

this的指向

類的方法內部如果含有this,他預設指向類的例項,但是,必須非常小心,一旦單獨使用該方法,可能會報錯;

class Logger {
  printName(name = 'there') {
    this.print(`Hello ${name}`);
  }

  print(text) {
    console.log(text);
  }
}

const logger = new Logger();
const { printName } = logger;
printName(); // TypeError: Cannot read property 'print' of undefined

Class 的取值函式(getter)和存值函式(setter)

與 ES5 一樣,在“類”的內部可以使用get和set關鍵字,對某個屬性設定存值函式和取值函式,攔截該屬性的存取行為

class MyClass {
  constructor() {
    // ...
  }
  get prop() {
    return 'getter';
  }
  set prop(value) {
    console.log('setter: '+value);
  }
}

let inst = new MyClass();

inst.prop = 123;
// setter: 123

inst.prop
// 'getter'

 

prop屬性有對應的存值函式和取值函式,因此賦值和讀取行為都被自定義了。

Class 的靜態方法

類相當於例項的原型,所有在類中定義的方法,都會被例項繼承。如果在一個方法前,加上static關鍵字,就表示該方法不會被例項繼承,而是直接通過類來呼叫,這就稱為“靜態方法”

class Foo {
  static classMethod() {
    return 'hello';
  }
}

Foo.classMethod() // 'hello'

var foo = new Foo();
foo.classMethod()
// TypeError: foo.classMethod is not a function

 

Foo類的classMethod方法前有static關鍵字,表明該方法是一個靜態方法,可以直接在Foo類上呼叫(Foo.classMethod()),而不是在Foo類的例項上呼叫。如果在例項上呼叫靜態方法,會丟擲一個錯誤,表示不存在該方法。
注意,如果靜態方法包含this關鍵字,這個this指的是類,而不是例項。

class Foo {
  static bar () {
    this.baz();
  }
  static baz () {
    console.log('hello');
  }
  baz () {
    console.log('world');
  }
}

Foo.bar() // hello

靜態方法bar呼叫了this.baz,這裡的this指的是Foo類,而不是Foo的例項,等同於呼叫Foo.baz。另外,從這個例子還可以看出,靜態方法可以與非靜態方法重名。
父類的靜態方法,可以被子類繼承。

class Foo {
  static classMethod() {
    return 'hello';
  }
}

class Bar extends Foo {
}

Bar.classMethod() // 'hello

靜態方法也是可以從super物件上呼叫的。

Class 的靜態屬性和例項屬性

靜態屬性指的是 Class 本身的屬性,即Class.propName,而不是定義在例項物件(this)上的屬性。

class Foo {
}

Foo.prop = 1;
Foo.prop // 1
//Foo類定義了一個靜態屬性prop,只有這種寫法可行,因為 ES6 明確規定,Class 內部只有靜態方法,沒有靜態屬性。

 

 (1)類的例項屬性
類的例項屬性可以用等式,寫入類的定義之中

class MyClass {
  myProp = 42;

  constructor() {
    console.log(this.myProp); // 42
  }
}
//myProp就是MyClass的例項屬性。在MyClass的例項上,可以讀取這個屬性。