簡體   English   中英

ES6注入擴展類

[英]ES6 Inject in an extended class

我很好奇我可以調用擴展類,讓它仍然導入它具體需要的東西。

歡迎班:

import { ErrorLevel } from './error-level.js';

export class Welcome extends ErrorLevel {
  constructor() {
    super();
  }
}

錯誤級別類別:

import { Notification } from 'aurelia-notification';

export class ErrorLevel {
  static inject() {
    return [Notification];
  }

  constructor(notification) {
    this.notification = notification;
  }
}

我知道一旦我調用super() ,它將調用擴展類並傳入0參數。 當我調用super()時,我的ErrorClass構造函數是否可以拉入Notification?

super([arguments]); // calls the parent constructor. super.functionOnParent([arguments]);

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/super

在Welcome的super函數中傳遞參數會使用該參數調用父類構造函數。 您將看到包含通知的日志, this日志設置為我們傳遞給super的參數。

http://jsbin.com/raguqopesu/1/edit?js,控制台,輸出

class ErrorLevel {
  constructor(notification) {
    this.notification = notification;
  }
}


class Welcome extends ErrorLevel {
  constructor() {
    super(Notification);
    console.log(this);
  }
}


const yo = new Welcome();
export class Welcome extends ErrorLevel {
  constructor(notification) {
    super(notification);
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM