在如此多的 JavaScript 库中,我将其global, factory视为函数的参数。
例如:
jQuery:
( function( global, factory ) {
"use strict";
if ( typeof module === "object" && typeof module.exports === "object" ) {
//...
Run Code Online (Sandbox Code Playgroud)
Vue.js:
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, global.Vue = factory());
}(this, function () { 'use strict';
/* */
// ...
Run Code Online (Sandbox Code Playgroud)
可能还有更多的例子......
我的问题是:为什么 …
我两个都看过:
throw new Error(error);
Run Code Online (Sandbox Code Playgroud)
&
console.error(error);
Run Code Online (Sandbox Code Playgroud)
例如:
jQuery:
if ( !w.document ) {
throw new Error( "jQuery requires a window with a document" );
}
Run Code Online (Sandbox Code Playgroud)
&
Vue.js:
if (config.warnHandler) {
config.warnHandler.call(null, msg, vm, trace);
} else if (hasConsole && (!config.silent)) {
console.error(("[Vue warn]: " + msg + trace));
}
Run Code Online (Sandbox Code Playgroud)
两种错误处理方式似乎都可靠且被使用。但我的问题是:
它们之间有区别吗?如果有,我什么时候应该使用哪个?
javascript ×2