一起使用Javascript命名空间闭包和原型失败了吗?

一起使用Javascript命名空间闭包和原型失败了吗?,javascript,prototype-programming,Javascript,Prototype Programming,我在项目中使用名称空间时遵循以下模式: // simply a namespace attic object // parent to my worker objects ;(function( RoaringSky, undefined ) { var opt = { backend : "" }, name = "" ; RoaringSky.init = function( options ) { jQuery.ex

我在项目中使用名称空间时遵循以下模式:

// simply a namespace attic object
// parent to my worker objects
;(function( RoaringSky, undefined ) 
{
    var opt = {
        backend : ""
    },
    name = ""
    ;

    RoaringSky.init = function( options ) {
       jQuery.extend(opt,options);
       console.log( 'RoaringSky.init complete');
    };
    // accessor 
    RoaringSky.opt = opt;

})(window.RoaringSky = window.RoaringSky || {});
和此命名空间的子对象,因此:

RoaringSky.Day = (function() {

    // constructor
    var Day = function(){

        var id = "none";
        var name = "none";
        var date = "none";
        var periods = new Array();

        this.periods = function() {
            return periods;
        };
    };

    // invoking day.time() fails - is not a function message
    Day.prototype.time = function() {
        return this.doSomthingWithDate(date);
    };


    return Day;
})(window.RoaringSky.Day = window.RoaringSky.Day || {});
我想,就目前而言,这个图案很好用。欢迎评论,但它似乎阻碍了原型属性的使用

也许我的理解是不完整的,我相信是的,但好吧,一旦我创建了一个对象 -就像我上面的日课一样,我应该能够以以下方式编写函数:

Day.prototype.time = function() {
    return this.doSomthingWithDate(date);
};
类的所有实例都将继承函数,因为它是构造函数的类对象Day的属性

但是,当我尝试这一点时:

var day = new Day();
day = new Date();
console.log( 'day.time: '+ day.time() );
我收到了“day.time不是函数”错误消息

我做错了什么?这开始让我发疯了

埃里克 两个问题:

RoaringSky.Day = (function() {

    // constructor
    var Day = function(){

        var id = "none";
        var name = "none";
        var date = "none";
        var periods = new Array();

        this.periods = function() {
            return periods;
        };
    };

    // invoking day.time() fails - is not a function message
    Day.prototype.time = function() {
        // can't use "date" here since it is not in scope
        return this.doSomthingWithDate(date);
    };


    return Day;
})(window.RoaringSky.Day = window.RoaringSky.Day || {});
然后:

我认为应该是var day=新的罗林斯基日;,您还将用新日期覆盖日期;,并且时间不是日期对象上的函数。如果执行day=new Date;,无论你在上什么日班,当天什么都不会留下。打电话给时间不起作用,原因和你约会时打电话给时间不正常是一样的。
var day = new Day(); // Day is not a member of window, so this will only work when used in the `RoaringSky.Day` declaration.
day = new Date();    // you then override the day variable for some reason
console.log( 'day.time: '+ day.time() ); // time is not a function member of Date