面向对象的 JavaScript 帮助

发布于 2024-09-08 11:57:38 字数 693 浏览 5 评论 0原文

我有以下代码:

var HD = function() { };

HD.Car = (function() {
    var _date = "09/07/2010";
    return {
        Make: undefined,
        Model: undefined,
        showMakeAndModel: function() {
            document.write(this.Make + " " + 
                           this.Model + 
                           " (data correct as of " + _date + ")");
            }
        };
    })();

var bert = new HD.Car();
bert.Make = "Ford";
bert.Model = "Capri";
window.onload = bert.showMakeAndModel();

并收到以下错误:

HD.Car 不是构造函数

我想做的就是测试(学习)带有闭包(对于私有成员)的“单例模式”,所以不是一个“真实”的例子,但我正在阅读的书表明这是这样做的方法。

所以有点困惑 - 任何帮助将不胜感激.. 抢

I have the following code:

var HD = function() { };

HD.Car = (function() {
    var _date = "09/07/2010";
    return {
        Make: undefined,
        Model: undefined,
        showMakeAndModel: function() {
            document.write(this.Make + " " + 
                           this.Model + 
                           " (data correct as of " + _date + ")");
            }
        };
    })();

var bert = new HD.Car();
bert.Make = "Ford";
bert.Model = "Capri";
window.onload = bert.showMakeAndModel();

And get the following error:

HD.Car is not a constructor

All I'm trying to do is test (to learn) the 'singleton pattern' with closure (for private members) so not a 'real' example, but the book I'm reading suggests this is the way to do it.

So a little confused - any help would be much appreciated..
Rob

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

鹿港巷口少年归 2024-09-15 11:57:38

HD.Car 类定义周围有一些不正确的 ()() 。这个固定示例的工作原理:

var HD = function() { }; 

HD.Car = function() { 
    var _date = "09/07/2010"; 
    return { 
        Make: undefined, 
        Model: undefined, 
        showMakeAndModel: function() { 
            document.write(this.Make + " " +  
                           this.Model +  
                           " (data correct as of " + _date + ")"); 
            } 
        }; 
    }; 

var bert = new HD.Car(); 
bert.Make = "Ford"; 
bert.Model = "Capri"; 
window.onload = bert.showMakeAndModel();

You have some incorrect ()() around the HD.Car class definition. This fixed sample works:

var HD = function() { }; 

HD.Car = function() { 
    var _date = "09/07/2010"; 
    return { 
        Make: undefined, 
        Model: undefined, 
        showMakeAndModel: function() { 
            document.write(this.Make + " " +  
                           this.Model +  
                           " (data correct as of " + _date + ")"); 
            } 
        }; 
    }; 

var bert = new HD.Car(); 
bert.Make = "Ford"; 
bert.Model = "Capri"; 
window.onload = bert.showMakeAndModel();
野却迷人 2024-09-15 11:57:38

有关更多信息:

如何用javascript编写单例类

希望这能为您带来更多启发!

For a bit more info:

How to write a singleton class in javascript.

Hopefully that will shed a bit more light for you!

物价感观 2024-09-15 11:57:38

在 JavaScript 中,您只能将 new 关键字与函数结合使用。

HD.Car 不是一个函数,它是一个对象。

只是不要在您的示例中使用 new

In JavaScript you can only use the new keyword with functions.

HD.Car is not a function, it's an object.

Just don't use new for your example.

眼眸里的那抹悲凉 2024-09-15 11:57:38

您的错误来自于您正在执行构造函数这一事实,如 HD.Car 声明之后(以及 var bert 之前)的左括号和右括号所示。因此,该函数正在执行并返回一个对象,然后您尝试将其与“new”运算符一起使用。

如果删除这些括号,我相信您会获得所需的功能。

Your error is coming from the fact that you're executing your constructor function, as indicated by the open and close parentheses after the declaration of HD.Car (and just before var bert). So, the function is executing and returning an object, which you're then trying to use with the "new" operator.

If you remove those parentheses, I believe you'll get the functionality you want.

星軌x 2024-09-15 11:57:38

删除 new,因为只有构造函数需要它。现在,HD.Car 将成为匿名且自动执行函数返回对象。然后删除 HD.Car 之后的括号。所以它应该看起来像:

var bert = HD.Car;

现在 HD.Car 是一个单例

如果你希望它更像一个工厂,你应该这样做:

HD.Car = function() {
    var _date = "09/07/2010";
    return {
        Make: undefined,
        Model: undefined,
        showMakeAndModel: function() {
            document.write(this.Make + " " + 
                           this.Model + 
                           " (data correct as of " + _date + ")");
        }
   };
};​

var bert = new HD.Car();

Remove new, because it is only needed for constructor functions. Now HD.Car will be the object that an anonymous and self-executing function returns. Then remove parens after HD.Car. So it should look like:

var bert = HD.Car;

Now HD.Car is a singleton.

If you want it to be more like a factory you should do:

HD.Car = function() {
    var _date = "09/07/2010";
    return {
        Make: undefined,
        Model: undefined,
        showMakeAndModel: function() {
            document.write(this.Make + " " + 
                           this.Model + 
                           " (data correct as of " + _date + ")");
        }
   };
};​

var bert = new HD.Car();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文