ES6从构造函数调用值

时间:2016-10-26 17:17:49

标签: javascript ecmascript-6

 class ServiceRecordGridFields{
  constructor(){
    this.allColumns = [
    {
      field:'assemblynumber',
      name:'Assembly',
      visible:false,
      width:175
    },{
      field:'assetid',
      name:'Asset ID',
      visible:false,
      width:110
    }];
  }
  getAllColumns(){

    return this.allColumns;
  }
}
export default ServiceRecordGridFields;

我有其他地方

import ServiceRecordGridFields from './_serviceRecordGridFields.js';


class serviceRecordGridsCtrl{

  constructor(){

this._ServiceRecordGridFields = ServiceRecordGridFields;

this._serviceRecordsResolve = serviceRecordsResolve;


  } 

  bclick(){
console.log(this._ServiceRecordGridFields.getAllColumns());
  }
  }

调用我得到的函数" this._ServiceRecordGridFields.getAllColumns不是函数"。

如果我添加"静态"在getAllColumns()之前,它返回undefined。我做错了什么?

2 个答案:

答案 0 :(得分:4)

ServiceRecordGridFields是一个类,所以你需要使用这个类来实例化一个对象:

this._ServiceRecordGridFields = new ServiceRecordGridFields()

答案 1 :(得分:1)

class ServiceRecordGridFields {
   constructor() {
     this.allColumns = [{
       field: 'assemblynumber',
       name: 'Assembly',
       visible: false,
       width: 175
     }, {
       field: 'assetid',
       name: 'Asset ID',
       visible: false,
       width: 110
     }];
   }
   getAllColumns() {

     return this.allColumns;
   }
 }

 class serviceRecordGridsCtrl {

   constructor() {

     this._ServiceRecordGridFields = new ServiceRecordGridFields();



   }

   bclick() {
     console.log(this._ServiceRecordGridFields.getAllColumns());
   }
 }

 var test = new serviceRecordGridsCtrl();
 test.bclick();

您需要实例化这两个类才能使用它们。

https://jsfiddle.net/68pep88x/1/