我正在制作一个迷你日历。我试图将适当的值传递到对象的 View 部分。但我在 View 部分一直未定义。我究竟做错了什么?
var date = {
monthsArray: ["January", "February","March","April","May","June","July","August", "September", "October", "November","December" ],
init: function() {
var fullDate = new Date();
this.dateParsed(fullDate);
this.view();
},
stringifyMonth: function(monthNumber) {
var monthName = this.monthsArray[monthNumber]
return monthName
},
dateParsed: function(fullDate){
var fullDate = new Date();
var monthNumber = fullDate.getMonth();
var dayNumber = fullDate.getDate();
var calanderYear = fullDate.getFullYear();
this.stringifyMonth(monthNumber)
return calanderYear
},
view: function(monthName){
console.log(this.stringifyMonth()) /// undefined
}
}.init()
最佳答案
有两件事:
- 将当前月份传递给
view
函数。您可以使用getMonth()
从日期对象获取月份
- 从
view
将同一月份传递给stringifyMonth
查看突出显示的更改和评论
var date = {
monthsArray: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
init: function() {
var fullDate = new Date();
this.dateParsed(fullDate);
// Get the current month from date object and pass to the view()
this.view(fullDate.getMonth()); // <-----
},
stringifyMonth: function(monthNumber) {
var monthName = this.monthsArray[monthNumber]
return monthName
},
dateParsed: function(fullDate) {
var fullDate = new Date();
var monthNumber = fullDate.getMonth();
var dayNumber = fullDate.getDate();
var calanderYear = fullDate.getFullYear();
this.stringifyMonth(monthNumber)
return calanderYear
},
view: function(monthName) {
// Pass the monthName to the stringifyMonth()
console.log(this.stringifyMonth(monthName)); // <-----
}
}.init();
关于javascript - 在对象内的函数之间传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33194828/