模块模式 javascript 不是函数?
Module pattern javascript not a function?
我正在尝试使我的 javascript 代码遵循我在此处遵循的模块模式:
http://addyosmani.com/resources/essentialjsdesignpatterns/book/#modulepatternjavascript
这是我目前的代码,除了运行时调用之外没有语法问题
loosetime.init() is not a function.
var loosetime = (function () {
var looseconfig = {
"format": "DD/MM/YY HH24:MM:SS.s",
"value": "DD/MM/YY 00:00.00",
"class": "input",
"delims": ['/', '-', '_', '.', '|', ',', ' ', ':']
};
function loosetime(a, b, c, d, e) {
var format = a;
var appendLoc = b;
var inputVal = c;
var inputName = d;
var inputClass = e;
var inputLength;
try {
if (typeof(format) == 'undefined') {
format = looseconfig.format;
} else {
format = parseDateTime(format);
}
try {
if (typeof(inputVal) == 'undefined') {
inputVal = looseconfig.value;
}
inputLength = inputVal.length - 2;
var input = document.createElement("input");
input.setAttribute("name", inputName);
input.setAttribute("maxlength", inputLength);
input.setAttribute("size", inputLength);
input.setAttribute("value", inputVal);
input.setAttribute("type", "input");
input.setAttribute("class", inputClass);
input.setAttribute("onkeypress", "dateTimeRules(event)");
input.setAttribute("onclick", "resetCursorPos(event)");
input.setAttribute("loosetime", format);
try {
var element = document.getElementById(appendLoc);
element.appendChild(input);
} catch (e) {
window.alert("Error, no Element given to append loosetime to.")
}
} catch (e) {
window.alert("Error, Value is invalid." + e.toString());
}
} catch (e) {
window.alert("Error, Date format missing or invalid.");
}
}
// other code here ...
return {
init: loosetime()
}
// end private closure then run the closure
});
理想情况下,我只想让 loosetime 工作,我不想显式调用构造函数。
例如loosetime("foo", "bar", "etc", "yolo", "123321");
我不确定我做错了什么,我需要 return 函数本身而不是别名吗?
return {
init: loosetime()
}
init
是 调用 loosetime
的 return 值(即 undefined
因为该函数没有 return
语句)。
如果您想分配函数本身而不是调用它,请删除 ()
。
你的第二个问题是揭示模块模式要求你运行闭包:
//end private closure then run the closure
})();
尽管有您的评论,但您错过了那里的 ()
。
有两点不对。
首先你需要确定你的模块是一个自调用匿名函数(SIAF),a.k.a立即调用函数表达式(IIFE)。
您可以找到有关此主题的更多信息 here。
其次,您不能在模块中调用 loosetime
函数,因为它不会 return 任何东西。您需要 init
键的值作为对 loosetime
函数的引用。
var loosetime = (function () {
// ...
function loosetime(a, b, c, d, e) {
// ...
}
// ...
return {
init: loosetime // Note that init has a reference to loosetime and we don't execute it.
};
})(); // Note that you're missing the execution parentheses.
现在您可以调用 init
并且模块中的 loosetime
函数将被执行。
我正在尝试使我的 javascript 代码遵循我在此处遵循的模块模式:
http://addyosmani.com/resources/essentialjsdesignpatterns/book/#modulepatternjavascript
这是我目前的代码,除了运行时调用之外没有语法问题
loosetime.init() is not a function.
var loosetime = (function () {
var looseconfig = {
"format": "DD/MM/YY HH24:MM:SS.s",
"value": "DD/MM/YY 00:00.00",
"class": "input",
"delims": ['/', '-', '_', '.', '|', ',', ' ', ':']
};
function loosetime(a, b, c, d, e) {
var format = a;
var appendLoc = b;
var inputVal = c;
var inputName = d;
var inputClass = e;
var inputLength;
try {
if (typeof(format) == 'undefined') {
format = looseconfig.format;
} else {
format = parseDateTime(format);
}
try {
if (typeof(inputVal) == 'undefined') {
inputVal = looseconfig.value;
}
inputLength = inputVal.length - 2;
var input = document.createElement("input");
input.setAttribute("name", inputName);
input.setAttribute("maxlength", inputLength);
input.setAttribute("size", inputLength);
input.setAttribute("value", inputVal);
input.setAttribute("type", "input");
input.setAttribute("class", inputClass);
input.setAttribute("onkeypress", "dateTimeRules(event)");
input.setAttribute("onclick", "resetCursorPos(event)");
input.setAttribute("loosetime", format);
try {
var element = document.getElementById(appendLoc);
element.appendChild(input);
} catch (e) {
window.alert("Error, no Element given to append loosetime to.")
}
} catch (e) {
window.alert("Error, Value is invalid." + e.toString());
}
} catch (e) {
window.alert("Error, Date format missing or invalid.");
}
}
// other code here ...
return {
init: loosetime()
}
// end private closure then run the closure
});
理想情况下,我只想让 loosetime 工作,我不想显式调用构造函数。
例如loosetime("foo", "bar", "etc", "yolo", "123321");
我不确定我做错了什么,我需要 return 函数本身而不是别名吗?
return { init: loosetime() }
init
是 调用 loosetime
的 return 值(即 undefined
因为该函数没有 return
语句)。
如果您想分配函数本身而不是调用它,请删除 ()
。
你的第二个问题是揭示模块模式要求你运行闭包:
//end private closure then run the closure
})();
尽管有您的评论,但您错过了那里的 ()
。
有两点不对。
首先你需要确定你的模块是一个自调用匿名函数(SIAF),a.k.a立即调用函数表达式(IIFE)。 您可以找到有关此主题的更多信息 here。
其次,您不能在模块中调用 loosetime
函数,因为它不会 return 任何东西。您需要 init
键的值作为对 loosetime
函数的引用。
var loosetime = (function () {
// ...
function loosetime(a, b, c, d, e) {
// ...
}
// ...
return {
init: loosetime // Note that init has a reference to loosetime and we don't execute it.
};
})(); // Note that you're missing the execution parentheses.
现在您可以调用 init
并且模块中的 loosetime
函数将被执行。