Coder Social home page Coder Social logo

enhance object literal about es6-code-style HOT 4 OPEN

otakustay avatar otakustay commented on June 8, 2024
enhance object literal

from es6-code-style.

Comments (4)

leeight avatar leeight commented on June 8, 2024

好像这个特性挺乱的

from es6-code-style.

otakustay avatar otakustay commented on June 8, 2024

涉及到解构就乱了,很多人是hold不了解构的

除了解构外,其它的我其实还是能接受的……计算属性是必须用,因为有Symbol这东西

from es6-code-style.

leeight avatar leeight commented on June 8, 2024

Symbol 这东西我还没有感受到啥明显的优势....

from es6-code-style.

otakustay avatar otakustay commented on June 8, 2024

我现在用Symbol主要是2个方法:

  1. 我真的想要个私有属性,真的不想让某些二货拿到,特别是在我这边不推荐_开头的属性命名
  2. 和decorators配合设置一些“本来不想要方法名”的方法
  3. 通用基类、外部介入的功能需要一个属性,但很担心和别的冲突,比如mini-event可以要一个属性管理所有事件,但因为使用场景太多总感觉起啥名字都有可能冲突,那不如上Symbol

提供个代码示例看看我们现在的玩法:

class CustomView extends BaseView {
  @on('table', 'sort')
  [Symbol('onTableSort')](e) {
    this.sortData(e.field);
    this.repaintTable();
  }

  @on('pager', 'pagechange')
  [Symbol('onPagerPageChange'])() {
    this.reloadData(this.pager.get('page'));
    this.repaintTable();
  }
}

我们不再找地方一行一行写get('xxx').on也不用uiEvents之类的属性了,全部用decoator往方法上加来绑定事件,看上去会更接近声明式编程一些,这种情况下,这些事件处理的方法是不想要任何名字的,也避免和其它的冲突,所以就用Symbol

from es6-code-style.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.