什么是'typeof define ==='function'&& define ['amd']'用于? [英] What is 'typeof define === 'function' && define['amd']' used for?

查看:2648
本文介绍了什么是'typeof define ==='function'&& define ['amd']'用于?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码的用途是什么?工厂功能在这做什么?这里的root是window对象。工厂是默认的java脚本功能吗?在什么样的场景中可以使用这种类型的代码。此代码来自Simon Tabor的 toggle.js 。 Zepto和ender是图书馆。这主要用于图书馆。

What purpose does the following code serve? What does factory function do here? Here root is window object. Is factory a default java script function? In what kind of scenarios this type of code can be used. This code is from toggle.js from Simon Tabor. Zepto and ender are libraries. Is this mostly used in libraries.

   if (typeof define === 'function' && define['amd']) {
        define(['jquery'], factory);
     } else {
      factory(root['jQuery'] || root['Zepto'] || root['ender'] || root['$']|| $);
    }


推荐答案

此代码检查是否存在 require.js ,一个JavaScript依赖关系管理库。

This code checks for the presence of require.js, a JavaScript dependency management library.

如果'define'是未定义,它是一个函数,'amd'(异步模块定义)也被定义,然后代码假定require.js正在播放。

If 'define' is not undefined and it is a function and 'amd' (asynchronous module definition) is also defined then the code assumes that require.js is in play.

如果是这样的话然后它定义'factory'并将jQuery作为依赖项传递给它。否则,它通过将代码附加到根对象来设置代码所需的依赖项。

If this is so then it defines 'factory' and passes jQuery to it as a dependency. Otherwise it sets up the dependencies that the code needs by attaching them to the root object.

至于'工厂'是什么:它不是由Javascript框架定义的,它最有可能是同一个文件中的一个函数。它将采用参数jQuery。

As for what 'factory' is: it is not defined by the Javascript framework, it will be a function in the same file most likely. It will take the parameter jQuery.

这篇关于什么是'typeof define ==='function'&& define ['amd']'用于?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆