使用‘新’关键字作为‘静态’的Javascript函数表达式是正确的吗?我只是想更深入地理解Javascript。我创建了一个“类”gameData我只想要其中之一,不需要构造函数,也不需要实例化。所以我就这样创造了它.。var gameData = new function () {
//May need this later
this.init = function () {
};
this.storageAvailable = function () {
if (typeof (Storage) !== "undefined") {
return true;
}
else {
return false;
}
};}意识到‘new’关键字不允许实例化它,并使它像静态类一样可用,这将在C#中。我想对了吗?是静态的?
3 回答
慕妹3242003
TA贡献1824条经验 获得超6个赞
new
this
new
new
var gameData = (function () { var public = { }, private = { }; // any private data can get stored here //May need this later public.init = function () { }; public.storageAvailable = function () { if (typeof (Storage) !== "undefined") { return true; } else { return false; } }; return public;}());
largeQ
TA贡献2039条经验 获得超7个赞
var gameData = { //May need this later init : function () { }, storageAvailable : function () { if (typeof (Storage) !== "undefined") { return true; } else { return false; } }}
var gameData = (function() { var private = 'private variable'; return { //May need this later init : function () { }, storageAvailable : function () { if (typeof (Storage) !== "undefined") { return true; } else { return false; } } }})();
添加回答
举报
0/150
提交
取消