[英]How ES6 classes compiled?
當我希望對本機動畫文檔做出反應時,我讀了一些有趣的文章。 代碼段:
class FadeInView extends React.Component {
state = {
fadeAnim: new Animated.Value(0), // Initial value for opacity: 0
}
// Other parts of code piece ...
// https://facebook.github.io/react-native/docs/animations.html
}
讓我們分解一下這里發生的事情。 在FadeInView構造函數中,將初始化為狀態的一部分的名為AnimationAnimation.Value的新動畫。
在上述聲明中,文檔提到狀態是在構造函數中初始化的。 但是,此代碼段中沒有構造函數。 根據其他語言(例如c#和c ++等)的知識,可以在沒有構造函數的類中聲明和初始化變量。
實際上將類屬性(一個js提議 )移到了構造函數中:
class FadeInView extends React.Component {
constructor() {
this.state = {
fadeAnim: new Animated.Value(0), // Initial value for
opacity : 0
};
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.