这部分JavaScript代码是什么意思
我需要知道这部分代码是什么意思,我不能特别理解这个条件这部分JavaScript代码是什么意思,javascript,Javascript,我需要知道这部分代码是什么意思,我不能特别理解这个条件 var classie = { // full names hasClass: hasClass, addClass: addClass, removeClass: removeClass, toggleClass: toggleClass, // short names has: hasClass, add: addClass, remove: removeClass, toggle: toggle
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;
}
这看起来很像require.js
在这种情况下,条件检查require.js的define
函数是否可用,并且是否具有名为amd
的非虚假属性,以使对象classie
作为模块可用,否则它将使其全局可用(至少在浏览器上下文中)通过将其附加到全局对象窗口
如果定义了RequireJS(1),则向其注册classie
。否则,它将放置在全局对象(窗口
)上
库作者有时会将此类代码放在库的底部,以使它们能够无缝地与它们所处的任何运行时环境(在本例中为RequireJS,使用全局对象是一种退路)
(1) 它的实际作用是:
typeof
运算符用于测试define
的类型是否为当前词汇环境中可用的值<之所以使用code>typeof,是因为它是唯一一个在与未声明的值一起使用时不抛出错误的运算符
define.amd
检查(确切地说:define上的amd
属性引用引用引用的值强制为真实值)提供了额外的信心,这实际上是RequireJS,而不是一些用户定义的对象。这个小条件使导出库成为可能(我不确定它是否是一个库,但它是否在AMD模块(因为我不使用require.js,所以我对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):
注意:如果您不想依赖JavaScript解释器来检查变量声明,我认为最起码的解决方案是检查它是否未定义(但属性/变量也可能未定义):
typeof variable!==“undefined”
它检查define
是否是一个函数,并且是否有一个名为amd
的非虚假属性。您有什么特别不明白的?在我看来,这看起来很像require.js。这不是我的问题,我对编辑投了赞成票,但我建议加上一个编辑:if子句条件要求的类型定义
为“函数”,而不是“属性”,但定义的类型是正确的正在测试全局对象上是否存在定义。在全局上下文中定义的变量(在本例中为定义
)最终成为全局对象上的属性。是的,我知道,但您没有确认它是否测试函数。而且属性可能有未定义的值。我是这样做的:“typeof运算符用于测试存在和类型…”@贝纳斯顿:我觉得你说的不错。我可能会放弃全局环境部分,因为typeof
确实检查当前环境(当然总是包含在全局环境中)。也就是说typeof
检查存在性(存在性?)还有define
的值。谢谢你的回答,不,它不是来自require.js只是Mary Lou创建的codrops SlidingHeaderLayout中的一个代码。请你向我解释一下:“define函数是可用的,它有一个名为amd的非虚假属性,可以使你的对象classie作为一个模块使用。”检查require.js是否存在只是为了提供与开发人员决定使用模块化方法进行编码的环境的兼容性(AMD异步模块定义).在这里解释这种方法太多了。这方面的书太多了。是的,我现在明白了开发人员为什么要使用此代码。我将阅读更多关于require.js的内容。非常感谢。是的,我认为你在这里说的都是正确的,我也有同样的问题。实际上,我需要更多的时间来检查this@ELHADRATI他们让我发这个,但肯定
// 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';
});