在没有构造函数的情况下将数据传递给ES7中的类

时间:2018-10-12 21:44:14

标签: javascript class ecmascript-7

我正在尝试使用新的ES7语法将属性传递给类,而不使用构造函数。

我知道我们可以使用构造函数来传递它:

 <!-- partial -->
  <div [ngClass]="{'container-fluid page-body-wrapper':authenticationService.isOnline()}">
    <!-- partial:partials/_sidebar.html -->
    <nav *ngIf="authenticationService.isOnline()" class="sidebar sidebar-offcanvas" id="sidebar">
      <ul class="nav">
        <li class="nav-item nav-profile">
          <a href="#" class="nav-link">
            <div class="nav-profile-image">
              <img src="assets/images/faces/face1.jpg" alt="profile">
              <span class="login-status online"></span> <!--change to offline or busy as needed-->
            </div>
            <div class="nav-profile-text d-flex flex-column">
              <span class="font-weight-bold mb-2">David Grey. H</span>
              <span class="text-secondary text-small">Project Manager</span>
            </div>
            <i class="mdi mdi-bookmark-check text-success nav-profile-badge"></i>
          </a>
        </li>
        <li class="nav-item">
          <a class="nav-link" routerLink="/tasks">
            <span class="menu-title">Dashboard</span>
            <i class="mdi mdi-home menu-icon"></i>
          </a>
        </li>
        <li *ngIf="authenticationService.isAdmin()" class="nav-item">
          <div class="dropdown">
            <a class="nav-link" data-toggle="collapse" (click)="OnClik3()" data-target="#myNavbar3">
              <span class="menu-title">Admin Area</span>
              <i class="menu-arrow"></i>
              <i class="mdi mdi-worker"></i>
            </a>
            <div class="collapse navbar-collapse" [ngClass]="{'show': buttontoggled3}" id="myNavbar3">
              <ul class="nav flex-column sub-menu">
                <li class="nav-item">
                  <a class="nav-link" data-toggle="dropdown" routerLink="/roles">Roles</a>
                </li>
                <li class="nav-item">
                  <a class="nav-link" data-toggle="dropdown" routerLink="/users">Users</a>
                </li>
                <li class="nav-item">
                  <a class="nav-link" data-toggle="dropdown" href="#">XXX API</a>
                </li>
              </ul>
            </div>
          </div>
        </li>
        <li *ngIf="authenticationService.isAdmin() || authenticationService.isCommercial()" class="nav-item">
        <div class="dropdown">
          <a class="nav-link" data-toggle="collapse" (click)="OnClik1()" data-target="#myNavbar1">
            <span class="menu-title">Network</span>
            <i class="menu-arrow"></i>
            <i class="mdi mdi-google-circles-extended"></i>
          </a>
          <div class="collapse navbar-collapse " [ngClass]="{'show': buttontoggled1}" id="myNavbar1">
            <ul class="nav flex-column sub-menu">
              <li class="nav-item">
                <a class="nav-link" data-toggle="dropdown" routerLink="/companies">Companies</a>
              </li>
              <li class="nav-item">
              <a class="nav-link" data-toggle="dropdown" routerLink="/contacts">Contacts</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" data-toggle="dropdown" href="#">Whatsapp API</a>
              </li>
            </ul>
          </div>
        </div>
        </li>

但是我们如何在ES7中使用“无构造函数”语法来做到这一点?

失败代码:

class MyClass {
    constructor(pacman) {
        this.pacman = pacman;
    }
}
...
const myInstance = new MyClass({food:'........'});

1 个答案:

答案 0 :(得分:2)

  

但是我们如何在ES7中使用“无构造函数”语法来做到这一点?

ES2016中没有无构造函数语法。如果您仍然参考实验性的class fields proposal,则类字段仅对每个实例都使用相同值初始化的事物有用,它不能依赖于传递给构造函数的任何东西。这就是您需要使用constructor的原因。只有在那里,您才能声明参数并使用它们来引用构造函数参数。