use strict leads to undefined function

…衆ロ難τιáo~ 提交于 2019-12-31 05:15:22

问题


I am trying to organize my js file and followed a suggested module pattern. When I use "use-strict" in this pattern a function is declared as undefined, without the "use-strict" method the function just works fine. Is the strict-mode recommended and when yes, why does the function not work with the use of it?

var envy = (function( $ ) {
   'use strict';

   /**
    * Viewport adjusments.
    *
    * @since 1.0.0
    */
   irp_viewportadjst = function() {
      $('meta[name="viewport"]').attr('content', 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0');
   },

   /**
    * Fire events on document ready, and bind other events.
    *
    * @since 1.0.0
    */
   ready = function() {
       irp_viewportadjst();
   };

   // Only expose the ready function to the world
   return {
       ready: ready
    };

   })( jQuery );
jQuery( envy.ready );

回答1:


Variable declaration hoisting is required in your code. Strict mode declaration demands your function name to be defined earlier. Either define your function name var irp_viewportadjst, ready; or Change the code as below

var envy = (function( $ ) {
   'use strict';

   /**
    * Viewport adjusments.
    *
    * @since 1.0.0
    */
   var irp_viewportadjst = function() {
      $('meta[name="viewport"]').attr('content', 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0');
   };

   /**
    * Fire events on document ready, and bind other events.
    *
    * @since 1.0.0
    */
   var ready = function() {
       irp_viewportadjst();
   };

   // Only expose the ready function to the world
   return {
       ready: ready
    };

   })( jQuery );
jQuery( envy.ready );


来源:https://stackoverflow.com/questions/44847221/use-strict-leads-to-undefined-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!