我正在创建具有大量属性的对象,我对实例化它们的最佳实践感到好奇.看起来真的很长的构造函数(实例化新对象并不好玩)似乎很糟糕.
function Book(title, author, pages, chapters, publisher, datePublished, authorHometown, protagonistFavoriteColor) {
this.title = title;
this.authorpages = authorpages;
this.pages = pages;
this.chapters = chapters;
this.publisher = publisher;
this.datePublished = datePublished;
this.authorHometown = authorHometown;
this.protagonistFavoriteColor = protagonistFavoriteColor;
}
// not reliable to remember how to order params
var rc = new Book("Robinson Crusoe", "Daniel Defoe", 342, 16, ...);Run Code Online (Sandbox Code Playgroud)
我想知道是否应该在构造函数中设置三个重要的属性(egtitle,author和pages),并为其余部分编写单独的setter.或者为了保持一致,我应该只使用setter吗?如果设置这种方式是最好的方法,JS是否有一种很好的方法来强制要求调用这些方法(类似于Java中的接口)?
function Book (title, author, pages){
this.title = title;
this.author = author;
this.pages = pages;
this.chapters = null;
this.publisher = null;
this.datePublished = null; …Run Code Online (Sandbox Code Playgroud)所以基本上我试图让这个渲染函数实时更新值(截至目前它只是在完成动画后跳转到最终值)
this.state = {
pleaseDisplayMe: Animated.value(0);
}
triggerAnimation(){
Animated.timing(
this.state.pleaseDisplayMe,
{toValue: 100,
duration: 5000}
).start();
}
render(){
return <Animated.Text>{this.state.pleaseDisplayMe}</Animated.Text>
}Run Code Online (Sandbox Code Playgroud)
我觉得因为这个库可以在引擎盖下动画值,然后必须有一种方法来显示发生了什么.或者是否可以通过样式提供某种内容/价值属性?
我已经尝试自定义编写我自己的setInterval函数,但我需要它与其他动画的时间排序更好,我喜欢访问RN的Easing库!
谢谢!