[英]JS Revealing module Pattern, What Am I doing Wrong
I've been trying to utilise the revealing module pattern, and thought I'd make a function for a non-css3 custom scroll bar. 我一直在尝试利用显示模块模式,并认为我会为非CSS3自定义滚动条提供功能。
var sb=(function($){
//Private
var settings=function(props){
return{
wrapper: props.wrapper ? document.getElementById(props.wrapper):null,
thumb: props.thumb ? document.getElementById(props.thumb):null,
track: props.track ? document.getElementById(props.track):null,
left: props.left ? props.left:null,
right: props.right ? props.right:null,
contentWidth: props.contentWidth? props.contentWidth:null
};
};
var LOG=function(input){
console.log(input)
};
//Object Literal
return{
Log:LOG,
settings:settings
};
})(jQuery);
The above is the module. 以上是模块。 I am setting the values like so:
我正在像这样设置值:
window.onload=function(){
sb.settings({wrapper:'bodyWrapper',thumb:'thumb',track:'track'});
}
HOWEVER, when I try to test it, I keep getting 'undefined' and other errors. 但是,当我尝试对其进行测试时,会不断出现“未定义”和其他错误。 Running
console.log(settings.wrapper)
inside the module returns undefined
. 在模块内部运行
console.log(settings.wrapper)
返回undefined
。 I really don't know where I'm going wrong, so any help would be much appreciated. 我真的不知道我要去哪里错,所以任何帮助将不胜感激。 Thanks in advance
提前致谢
Running
console.log(settings.wrapper)
inside the module returnsundefined
.在模块内部运行
console.log(settings.wrapper)
返回undefined
。
Of course, because settings
is the function object (which doesn't have a wrapper
property) and not the object is has returned when it was called. 当然,因为
settings
是函数对象(没有wrapper
属性),并且在调用该对象时未返回该对象。 You seem to want 你好像要
var sb = (function(){
// private variables
var settings = {};
// public (exported) properties
return {
log: function(input) {
console.log(input);
},
getSetting: function(name) {
return settings[name];
},
setSettings: function(props){
settings = { // assign to the private variable!
wrapper: props.wrapper ? document.getElementById(props.wrapper) : null,
thumb: props.thumb ? document.getElementById(props.thumb) : null,
track: props.track ? document.getElementById(props.track) : null,
left: props.left || null,
right: props.right || null,
contentWidth: props.contentWidth || null
};
return true;
}
};
})();
window.onload = function(){
sb.setSettings({wrapper:'bodyWrapper',thumb:'thumb',track:'track'});
sb.log(sb.getSetting("wrapper"));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.