简体   繁体   English

将函数包装到匿名函数中后,为什么函数不起作用?

[英]Why don't functions work after wrapping them in an anonymous function?

I was told to avoid public variables and conflicts, it is better to place the whole plugin in an anonymous function. 我被告知要避免公共变量和冲突,最好将整个插件放在匿名函数中。 I tried to do this but functions do not work anymore. 我试图这样做,但是功能不再起作用。

Here is a simple example: 这是一个简单的示例:

 (function($) { function changeIt() { $("button").text("off"); } }(jQuery)); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button onclick="changeIt()">On</button> 

the function runs by a HTML element but since it is inside another function, the element cannot find it. 该函数由HTML元素运行,但是由于它位于另一个函数中,因此该元素找不到它。

1- How can I make it working? 1-我如何使其工作?

2- Is it a good approach to make sure public variables are covered and assumed as private ones? 2-确保覆盖公共变量并将其假定为私有变量是一种好方法吗?

Thanks 谢谢

This has to do with scope. 这与范围有关。 The changeIt function only exists within the function($). changeIt函数仅存在于function($)中。 If you want to add it to public scope its best to create an object the prototype the functions. 如果要将其添加到公共范围,则最好创建对象原型的功能。

(function($) {}(jQuery)); -> This will create a private scope, basically your function won't be defined inside window , but inside this private scope. ->这将创建一个私有范围,基本上您的函数不会在window内定义,而是在此私有范围内定义。

<button onclick="changeIt()">On</button> -> This will try to execute changeIt from window , which will be undefined . <button onclick="changeIt()">On</button> ->这将尝试changeIt undefined window执行changeIt

It's because the function is no more in the global object and thus the onclick event handler in the html element cannot find it by name. 这是因为该函数不再存在于全局对象中,因此html元素中的onclick事件处理程序无法通过名称找到它。

Change your html to 将您的html更改为

<button id="mybutton">On</button>

and your code to 和你的代码

(function($) {

  $("#mybutton").click(function(){
    $("#mybutton").text("off");
  });

}(jQuery));

and it will work because the handler will not need to be looked up by name 它将起作用,因为不需要按名称查找处理程序

inline events defined on elements only have access to the global scope, so, when you moved that function out of the global scope and into the scope created by the anonymous function, it was no longer accessible to the inline event. 在元素上定义的内联事件只能访问全局范围,因此,当您将该函数移出全局范围并移入匿名函数创建的范围时,内联事件将不再可访问它。

You have two options: 您有两种选择:

Stop using inline events 停止使用内联事件

(function($) {

  function changeIt() {
    $("button").text("off");
      }
      $("button").click(changeIt);

 }(jQuery));

Or define it globally 或全局定义

//(function($) {

  function changeIt() {
    $("button").text("off");
  }

//}(jQuery));

Since changeIt is not found in the global scope ( window in the case of a browser), the function isn't triggered on a click. 由于在全局范围(在浏览器中为window中未找到changeIt ,因此不会在单击时触发该功能。 In fact, your console should show an exception like: "Uncaught ReferenceError: changeIt is not defined". 实际上,您的控制台应显示类似如下的异常:“未捕获的ReferenceError:未定义changeIt”。

To remedy this, and keep your function structure, set the handler from within the "onload" handler: 要解决此问题并保持函数结构,请在“ onload”处理程序中设置处理程序:

 (function($) { $('#myBtn').on('click', function () { $("button").text("off"); }); }(jQuery)); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="myBtn">On</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM