Tag: dynamictypes

在JavaScript中dynamicinput:这是一个好主意吗?

直到最近,我还没有在JavaScript等dynamictypes语言中做过很多编码,现在我开始明白什么是可能的,我开始想知道什么是好主意,什么是好主意。 具体来说,我不确定是否将一个variables的types作为一个函数通过一系列的操作进行改变是一种很好的做法。 例如,我有一堆包含date作为string的文件。 我使用前端提取date属性并将其存储在代表原始文件的对象中。 这些string本身并不是非常一致,所以我使用Moment.js来parsing它们,并将结果存储在同一个对象article.date的同一个属性中。 这对我来说或多或less是正确的,因为article.date在被parsing并存储为Date /'Moment'types之前只是一个操作的string。 接下来是我有点不确定的部分。 这是ExpressJS应用程序的一部分,因此这些对象的数组作为render()调用中的数据传入,并转到Jade模板进行渲染。 但是如果我想在Moment.js中使用显示方法来控制date看起来像一个string? 在将它传入之前,将date属性的types重新更改为一个String是否合理? 例: articles[i] = processArticle(content); // creates an article object from YAML, object has a property article.attributes.date articles[i].attributes.date = moment(articles[i].attributes.date); // attribute is now a Date/Moment articles[i].attributes.date = articles[i].attributes.date.format("dddd, MMMM Do YYYY, h:mm:ss a"); // attribute is now "Sunday, February 14th 2010, 3:25:50 pm"