面向对象的JavaScript:你会怎么做?

dun*_*unc 5 javascript oop

因为我已经更好地了解了JS,我已经从程序风格转变为半OO(不要问我的意思是什么:基本上是混乱!)但是现在我想开始正确地使用它.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";
Run Code Online (Sandbox Code Playgroud)
  • 关键:根据学校日历的周数
  • 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 = [];
Run Code Online (Sandbox Code Playgroud)

但是我看得越多,感觉就越不正确!

我应该创建"Week"对象,然后是一个容器,它有一个返回所有Week对象的方法吗?我一直在阅读John Resig的"Pro JavaScript Techniques"中的OOP章节,但事实是我不完全理解它.这感觉就像是正确的方法,但是对象中的对象正在伤害我的头脑.

最后的结果应该是我在我的一个页面上包含这个脚本,然后可以使用类似的东西var WeeksArray = LEAP.Schedule.getWeeks();,但即使这样我也不确定这是否真实?

我很困惑......!:D对此主题的任何帮助将非常感激.

Rob*_*b W 2

setWeeks: function(){
  var WeeksArray = []; //This variable is private, which is probably not your desired result.
}
Run Code Online (Sandbox Code Playgroud)

^ 这不起作用,请参阅评论。

我推荐这样的东西:外部文件:

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;
}
Run Code Online (Sandbox Code Playgroud)

主要文件:

var Scheduleobject = new LEAP.Schedule();
var weeks = Scheduleobject.getWeeks();
Run Code Online (Sandbox Code Playgroud)

对我来说,这感觉像是一种非常自然的 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.
Run Code Online (Sandbox Code Playgroud)

周类可以这样调用:

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`
Run Code Online (Sandbox Code Playgroud)