这部分JavaScript代码意味着什么

时间:2016-12-14 21:48:48

标签: javascript

请我知道这部分代码是什么意思,我不能特别理解这个条件

var classie = {
  // full names
  hasClass: hasClass,
  addClass: addClass,
  removeClass: removeClass,
  toggleClass: toggleClass,
  // short names
  has: hasClass,
  add: addClass,
  remove: removeClass,
  toggle: toggleClass
};

// transport
if ( typeof define === 'function' && define.amd ) {
  // AMD
  define( classie );
} else {
  // browser global
  window.classie = classie;
}

3 个答案:

答案 0 :(得分:1)

这对我来说看起来很像 require.js

  

http://requirejs.org/

在这种情况下,条件检查require.js的$3CXConfig函数是否可用,并且具有名为define的非虚假属性,以使对象amd可用作模块,否则它会将全局(至少在浏览器上下文中)提供给全局对象 classie

答案 1 :(得分:1)

如果定义了RequireJS(1),则向其注册classie。否则它将被放置在全局对象(window)上。

图书馆作者有时会将这种代码放在他们库的底部,以使他们能够无缝地与他们发现的任何运行时环境(在这种情况下为RequireJS,使用全局对象的后退)。

(1)它实际上做了什么:

typeof运算符用于测试define的类型作为当前词法环境中可用的值。使用typeof是因为它是与未声明的值一起使用时不会抛出错误的唯一运算符。

define.amd检查(确切地说:amd define上的Date Min Max Count 1/1/2015 0.28 6.02 13 2/1/2015 0.2 7.72 8 3/1/2015 1 1 1 4/1/2015 0.4 6.87 7 5/1/2015 0.36 3.05 8 6/1/2015 0.17 1.26 13 7/1/2015 0.31 1.59 15 8/1/2015 0.39 3.35 13 9/1/2015 0.22 0.86 10 10/1/2015 0.3 2.48 13 11/1/2015 0.16 0.82 9 12/1/2015 0.33 2.18 5 1/1/2016 0.23 1.16 14 2/1/2016 0.38 1.74 7 3/1/2016 0.1 8.87 9 4/1/2016 0.28 0.68 3 5/1/2016 0.13 3.23 11 6/1/2016 0.33 1 5 7/1/2016 0.28 1.26 4 8/1/2016 0.08 0.41 2 9/1/2016 0.43 0.61 2 10/1/2016 0.49 1.39 4 11/1/2016 0.89 0.89 1 属性引用所引用的值强制为真值)可以让人更加确信这实际上是RequireJS而不是某些用户定义的对象。

答案 2 :(得分:0)

这种小的条件可以导出您的图书馆(我不确定它是否是图书馆,但它在AMD模块之间并不重要)(我知道)几乎没有关于require.js的事情,因为我没有使用它)和全局JavaScript对象。

我主要是要回答这个问题,因为我想纠正@BenAston。让我们更好地检查一下情况:

// typeof is special with an unique identifier argument,
// however it's not truth that it's made to check if a variable
// is declared (since its value type can be any).

// Since typeof is special with identifiers, independently of strict mode
// it doesn't throw exceptions for non-existent variables (identifiers...).

If IsTruth (

    IsTruth(
    (UnaryTypeOf(Identifier define) Strict—Is 'function') And

    // Now check if define.amd getter returns a truth value.
    IsTruth(
        Get(Identifier define, '\u{64}md')
    ))

    CONSEQUENT: Define running script module with classie (AMD case)
        (Note that I'm not sure if that's what AMD does, I don't use it).

如果情况不是这样,那么您的脚本最终会运行window.classie setter,并引用已提升的classie

我的自定义方式(我不需要再次编写,因为我使用webpack)以更通用的方式执行等效操作(我建议使用webpack):

// Note: global object could be 'self' instead of 'this', not sure if
// all JavaScript based-platforms support it, though...
// No problem to use this.

(functio\u{6e} universalModuleDefinition(global, factory) {
    'use strict';

    // More readable with block statements.

    // I worry about precedence.
    if (('function' === typeof define) && define.amd) {
        define([exports], factory);
    }
    else if (exists('exports')) {
        factory(exports);
    }
    else {
        factory(global.classie = {});
    }

    // Check for variable declaration (independently of its value).
    // eval is better than Function# constructor since it evaluates a string
    // using the scope where it got called.

    function exists(IdentifierName) {
        try {
            return eval(IdentifierName), true;
        } catch(e) {
            return false;
        }
    }

})(this, function factory(classie) {
    // classie = your exports (your library namespace, for example)
    'use strict';
});

注意:如果您不想依赖JavaScript解释器来检查变量声明,我认为最小的解决方案是检查它是否不是undefined(而是属性/变量)也可能是undefined):typeof variable !== 'undefined'