使用严格导致未定义的函数

use strict leads to undefined function

我正在尝试组织我的 js 文件并遵循建议的模块模式。当我在此模式中使用 "use-strict" 时,函数被声明为未定义,如果没有 "use-strict" 方法,该函数就可以正常工作。是否推荐使用严格模式,如果是,为什么该功能无法使用它?

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 );

您的代码中需要变量声明提升。严格模式声明要求您的函数名称更早定义。 要么定义你的函数名称 var irp_viewportadjst, ready; 或 更改代码如下

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 );