随着我对 JS 的了解越来越深,我已经从过程式风格转向半面向对象风格(不要问我那是什么意思:基本上是一团糟!)但现在我想开始使用它适本地。 OO 吸引了我的编码大脑。
但是,我正在尝试开发一个学校周库,但我不确定我最好怎么做。
如果我简单地使用一个星期数组,它们看起来像这样:
WeeksArray[36].StartDate = "2011-09-05";
WeeksArray[36].EndDateSchool = "2011-09-09";
WeeksArray[36].EndDateProper = "2011-09-11";
WeeksArray[36].JSDate = new Date ( 2011, 8, 05 );
WeeksArray[36].Type = "1";
WeeksArray[36].Label = "Week 36: 5th Sept 2011";
- 键:根据校历的周数
- StartDate/EndDate:MySQL 兼容的日期范围
- JSDate:一周开始的JS日期对象
- 类型:学校时间表,第 1 周或第 2 周
- 标签:指示一周开始的人类可读标签
我希望其他脚本可以访问此库,以便它们可以加载包含学校日历中所有周的数组或对象。例如,我想象我的一个脚本会根据这些信息生成一个下拉菜单,该菜单显示“第 36 周:2011 年 9 月 5 日”,点击后会向我的 PHP 脚本和 SQL 数据库发送请求,然后过滤信息相应地在屏幕上。 注意:我不需要有关后者实现的帮助,它只是上下文的示例。
我开始编码如下:
var LEAP = {}
LEAP.Schedule = {
init: function() {
this.setWeeks();
}
setWeeks: function() {
var WeeksArray = [];
但是越看越觉得不对!
我应该创建“Week”对象,然后为它们创建一个容器,该容器具有返回所有 Week 对象的方法吗?我一直在阅读 John Resig 的“Pro JavaScript Techniques”中的 OOP 章节,但说实话我并不完全理解它。这感觉是正确的方法,但是对象中的对象让我头疼。
最终结果应该是我将这个脚本包含在我的一个页面上,然后可以使用类似 var WeeksArray = LEAP.Schedule.getWeeks();
的东西,但即便如此我也不是确定这是现实的吗?
我很困惑...! :D 非常感谢有关该主题的任何帮助。
最佳答案
setWeeks: function(){
var WeeksArray = []; //This variable is private, which is probably not your desired result.
}
^ 这不起作用,请参阅评论。
我推荐这样的东西: 外部文件:
var LEAP = {};
LEAP.Schedule = function(){//init
//all events which should only occur once should be called here
//Create the initial objects, e.g.
this.weeks = [];
this.calculateWeeks();
}
LEAP.Schedule.protoype.calculateWeeks = function(){
for (var i=0; i<52; i++){
this.weeks.push(Math.random()); //For the sake of the example ;)
}
}
LEAP.Schedule.prototype.getWeeks = function(){
return this.weeks;
}
主文件:
var Scheduleobject = new LEAP.Schedule();
var weeks = Scheduleobject.getWeeks();
对我来说,这感觉像是一种非常自然的 OOP 方法。 您甚至可以更改 LEAP.Schedule 函数,使其根据情况立即返回 Weeks 数组。
编辑
一周课的例子:
LEAP.Schedule.week = function(n_year, n_month, n_day, n_week){
//add code to validate the input
//...
//finished validating, processing:
this.year = n_year;
this.month = n_month;
this.day = n_day;
this.week = n_week;
}
LEAP.Schedule.week.protoype.getStartDate = function(){
return year + "-" + pad(month) + "-" + pad(day);
}
//LEAP.Schedule.week.prototype.*Date are defined in a similar way
//The "EndDateSchool" and "EndDateProper" variables always follow the same pattern. Reduce the number of unnecessary variables by calculating these variables in the prototype function.
LEAP.Schedule.week.prototype.getLabel = function(){
return "week" + this.week + ": " + this.day + (day==1||day==21||day==31?"st":day==2||day==22?"nd":day==3||day==23?"rd":"th") + " " + ["jan", "feb", "mar", "etc"][this.month-1] + " " + this.year;
}
function pad(n){return n>9?n:"0"+n}//a simple function to add a zero for your specific cases.
周类可以这样调用:
var week = new Schedule.Week(2011, 8, 5, 36); //or this.Week(2011, 8, 5, 36) from the contex of the class.
var startDate = week.getStartDate(); //example`
关于javascript - 面向对象的 JavaScript : How would you go about this?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7387077/