search

Home  >  Q&A  >  body text

javascript - Problem with !function()

!function(){} is placed in the js file. How to call the method inside after referencing it?

曾经蜡笔没有小新曾经蜡笔没有小新2859 days ago534

reply all(4)I'll reply

  • 为情所困

    为情所困2017-05-19 10:15:21

    // test.js
    !function(){
      var a = function(){console.log(1)}
      this.demo = a;                                                       
    }();
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
    <html>
    <head>
    <script src="test.js"></script>
    </head>
    <body>
    <SCRIPT>
    demo()                                                                 
    </SCRIPT>
    </body>
    </html>

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-19 10:15:21

    Just return the internal method

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-05-19 10:15:21

    !function(){} is not a method, but an expression with a value of false.

    reply
    0
  • 怪我咯

    怪我咯2017-05-19 10:15:21

    !function means executing the function immediately. You need to see if there are any methods open to the outside world inside the function, such as modifying the properties of the window object

    reply
    0
  • Cancelreply