JQuery 自动完成而不污染全局命名空间
所以我有一个 Jquery 自动完成的工作解决方案,但我的解决方案确实污染了全局名称空间。我尝试将其封装在一个对象中,但遗憾的是该对象中的函数无法访问。
这是我的例子:
var Source = [ 'Foo', 'Bar' , 'Baz'];
function Focused() {
console.log("Focused");
};
function Selected() {
console.log("Selected");
};
var autcompleteOptions = {
minLength: 1,
source: Source,
select: Selected,
focus: Focused,
position: { collision: "flip" }
};
function setup() {
$("#Steelgrade").autocomplete(autcompleteOptions)
.bind('focus', function () {
$("#Steelgrade").autocomplete("search");
});
}
setup();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js" integrity="sha512-uto9mlQzrs59VwILcLiRYeLKPPbS/bT71da/OEBYEwcdNUk8jYIy+D176RYoop1Da+f9mvkYrmj5MCLZWEtQuA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<input type="text" id="Steelgrade"/>
编辑:如果我尝试将其封装在一个对象中,则现在定义了具有自动完成功能的函数:
var Steelautocomplete = (function(){
var Source = [ 'Foo', 'Bar' , 'Baz'];
var Focused = function() {
console.log("Focused");
};
var Selected = function() {
console.log("Selected");
};
var autcompleteOptions = {
minLength: 1,
source: Source,
select: Selected,
focus: Focused,
position: { collision: "flip" }
};
var setup = function () {
$("#Steelgrade").autocomplete(autcompleteOptions)
.bind('focus', function () {
$("#Steelgrade").autocomplete("search");
});
};
return{
Setup:setup
};
})();
Steelautocomplete.Setup();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js" integrity="sha512-uto9mlQzrs59VwILcLiRYeLKPPbS/bT71da/OEBYEwcdNUk8jYIy+D176RYoop1Da+f9mvkYrmj5MCLZWEtQuA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<input type="text" id="Steelgrade"/>
有人能指出我的上下文哪里出了问题吗?
编辑:感谢您指出模块和命名空间之间的区别。我将第二个示例更改为工作代码作为命名空间变体。
So I have got a working solution for Jquery Autocomplete, but my Solution does pollute the global namespace. I tried to encapsule it within an object, but sadly functions withing the object are not be able to accessed.
Here my example:
var Source = [ 'Foo', 'Bar' , 'Baz'];
function Focused() {
console.log("Focused");
};
function Selected() {
console.log("Selected");
};
var autcompleteOptions = {
minLength: 1,
source: Source,
select: Selected,
focus: Focused,
position: { collision: "flip" }
};
function setup() {
$("#Steelgrade").autocomplete(autcompleteOptions)
.bind('focus', function () {
$("#Steelgrade").autocomplete("search");
});
}
setup();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js" integrity="sha512-uto9mlQzrs59VwILcLiRYeLKPPbS/bT71da/OEBYEwcdNUk8jYIy+D176RYoop1Da+f9mvkYrmj5MCLZWEtQuA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<input type="text" id="Steelgrade"/>
Edit: If i try to encasule it within an object , the functions with autocomplete are now defined:
var Steelautocomplete = (function(){
var Source = [ 'Foo', 'Bar' , 'Baz'];
var Focused = function() {
console.log("Focused");
};
var Selected = function() {
console.log("Selected");
};
var autcompleteOptions = {
minLength: 1,
source: Source,
select: Selected,
focus: Focused,
position: { collision: "flip" }
};
var setup = function () {
$("#Steelgrade").autocomplete(autcompleteOptions)
.bind('focus', function () {
$("#Steelgrade").autocomplete("search");
});
};
return{
Setup:setup
};
})();
Steelautocomplete.Setup();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js" integrity="sha512-uto9mlQzrs59VwILcLiRYeLKPPbS/bT71da/OEBYEwcdNUk8jYIy+D176RYoop1Da+f9mvkYrmj5MCLZWEtQuA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<input type="text" id="Steelgrade"/>
Could anybody point out where i did go wrong with my context ?
Edit: Thanks for pointing out the difference between module and namespace. I changed my second example to working code as namespace variant.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论