在backbone.js的前几行中,我不明白他们为什么要在export或者require上测试undefined
很明显,由于他们没有设置,因此它将是未定义的.如果它是一个全局(窗口)对象,那么他们会明确地说出来.
root.exports // they don't do this
root.require
为什么他们检查这个?
typeof exports !== 'undefined'
还有这个
if (!_ && (typeof require !== 'undefined'))
而这来自上面
!_
完整片段
(function(){
var root = this,prevIoUsBackbone = root.Backbone,slice = Array.prototype.slice,splice = Array.prototype.splice,_ = root._,Backbone;
if (typeof exports !== 'undefined') {
Backbone = exports;
} else {
Backbone = root.Backbone = {};
}
Backbone.VERSION = '0.9.2';
if (!_ && (typeof require !== 'undefined')) {
_ = require('underscore');
}
最佳答案
那就是允许Backbone.js用作我相信的Common.js模块.更多细节:http://wiki.commonjs.org/wiki/Modules/1.1
原文链接:https://www.f2er.com/js/429636.html特别是这一点:
In a module,there is a free variable called “exports”,that is an object that the module may add its API to as it executes.
此外,这一点涵盖了您关于require的问题:
In a module,there is a free variable “require”,that is a Function. The “require” function accepts a module identifier. “require” returns the exported API of the foreign module.