鍍金池/ 問答/HTML/ transform-class-properties 的配置問題?

transform-class-properties 的配置問題?

transform-class-properties 主要是解決 es6 中使用class聲明中 :defaultProps={} 不支持的問題。

需要使用 stage-0 或者 該插件轉(zhuǎn)義。

問題:

{

spec:true //這個和默認(rèn)的false啥區(qū)別呢????

}

參考:官方原鏈接。沒看懂

https://babeljs.io/docs/plugi...

回答
編輯回答
呆萌傻

其實題主自己寫個腳本跑一跑就行了:

1.js

class Book {
  title = 'a';

  static cover;
}

const a = new Book();
a.title = 'The Song of ice and fire';

.babelrc

{
  "presets": [
    "env"
  ],
  "plugins": [
    ["transform-class-properties", { "spec": false }]
  ]
}
babel 1.js -o 1.false.js 
babel 1.js -o 1.true.js  // 然后修改上面的 `spec`

對比一下生成的兩個文件,其實正如文檔中所寫的那樣:

當(dāng) spectrue 時,

  1. 使用 Object.defineProperty 取代 title='a' 這樣的賦值操作。
  2. 靜態(tài)變量(cover)即使沒有初始值,也會創(chuàng)建。
2017年12月25日 19:36