gpt4 book ai didi

javascript - 面向对象的 JavaScript 帮助

转载 作者:行者123 更新时间:2023-11-29 18:35:53 24 4
gpt4 key购买 nike

我有以下代码:

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 is not a constructor

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

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

最佳答案

您在 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();

关于javascript - 面向对象的 JavaScript 帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3213909/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com