我正在定义要使用的单独样式,并希望设置默认值,因为现在代码是多余的。 当然,肯定有一种比不断重复“fontFace:'Sen'”更好的方法来解决这个问题。它是什么?
var styles={};
styles.title={ x:0, y:0, fontFace: 'Sen', fontSize:28, color:'#7700dd', valign: 'top',};
styles.author={ x:1, y:1, fontFace: 'Sen', fontSize:12, color:'#363636' };
styles.comment={ x:2, y:3, fontFace: 'Sen', fontSize:8, color:'#363636' }
它会像这样使用:
slide.addText(myTitle, styles.title);
slide.addText(myAuthor, styles.author);
slide.addText(myComment, styles.comment);
最佳答案
是的 - 只需有一个默认对象,首先传播它,然后如果存在任何重复的属性,它们将被覆盖。
const defaults = { fontFace: "Sen" /* All other properties */ };
styles.title = { ...defaults, /* All other properties - if duplicates, new values are set */ };
关于javascript - 如何为对象实例设置默认键值对?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56845476/