简体   繁体   English

Javascript 对象到 ES6 类

[英]Javascript Object to ES6 Classes

I have a javascript plugin written in vanilla javascript in Object Notation我有一个 javascript 插件,用对象表示法中的 vanilla javascript 编写

example例子

start = {
  config: {
    a : 1
  },
  core: {
    engine_part1:function() {

    },
    engine_part2: function() {
    } 
  }
  init: function(){

  }
}

In this Object Notation the functions can be declare inside core ie, engine_part1() and engine_part2()在这个对象表示法中,函数可以在core内部声明,即 engine_part1() 和 engine_part2()

Is there any workarounds to achieve the same with Javascript Classes?是否有任何解决方法可以通过 Javascript 类实现相同的目标?

In Javascript classes the class have datas and functions but I couldn't write functions inside an object like core in object notation.在 Javascript 类中,该类具有数据和函数,但我无法在对象表示法中的core对象内编写函数。

Thanks in advance提前致谢

Simply use the class statement to declare your class, then add its properties in the constructor and its (static ) methods in the class' body.只需使用class语句来声明您的类,然后将其属性添加到constructor并将其(static方法添加到类的主体中。

 class Start { constructor() { this.config = { a: 1 }; this.core = { engine_part1: () => (console.log('engine_part1')), engine_part2: () => (console.log('engine_part2')), } } init() { console.log('init'); } } const start = new Start; console.log(start.config); start.core.engine_part1(); start.init();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM