Objective
Underscore.js is a very lean library, compressed only 4KB. Underscore provides more than 100 functions, including commonly used: map, filter, invoke-of course, there are more professional auxiliary functions, such as: function binding, JavaScript template function, create quick index, strong type equality test, and so on. Make up for the deficiencies of the standard library, greatly facilitate the programming of JavaScript.
Micro-mail programs cannot be invoked directly using require (' underscore.js ').
The modularization mechanism of the micro-letter Small program
The operation Environment of micro-credit program supports COMMOJS modularization, through Module.exports exposed objects, through require to obtain objects.
Micro-Letter Applet Quick Start Utils/util.js
function Formattime (date) {
var year = date.getfullyear ()
var month = Date.getmonth () + 1
var day = Date.getda Te ()
var hour = date.gethours () var
minute = Date.getminutes ()
var second = Date.getseconds ();
return [year, month, Day].map (FormatNumber). Join ('/') + ' + [hour, Minute, Second].map (FormatNumber). Join (': ')
}< C9/>function FormatNumber (n) {
n = n.tostring () return
n[1]? N: ' 0 ' + N
}
module.exports = {
fo Rmattime:formattime
}
Pages/log/log.js
var util = require ('.. /.. /utils/util.js ')
Page ({
data: {
logs: []
},
onload:function () {
This.setdata ({
logs: (Wx.getstoragesync (' logs ') | | []). Map (function (log) {return
util.formattime (new Date (log)
})
}})
Cause analysis
Underscore COMMONJS module export code is as follows:
Export the underscore object for **node.js**, with
//backwards-compatibility for the old ' require () ' API. If we ' re
in//The browser, add ' _ ' as a global object.
if (typeof exports!== ' undefined ') {
if (typeof module!== ' undefined ' && module.exports) {
exports = MoD Ule.exports = _;
}
Exports._ = _;
} else {
Root._ = _;
}
Exports and module must all have definitions in order to be exported. By testing, the operation environment exports and module of the micro-letter applet are not defined
Index.js
//Get application instance
var app = Getapp ();
Page ({
onload:function () {
console.log (' onLoad ');
var that = this;
Console.log (' typeof exports: ' + typeof exports);
Console.log (' typeof module: ' + typeof exports);
var MyClass = function () {
}
module.exports = MyClass;
Console.log (' typeof module.exports: ' + typeof module.exports);
}
)
Solving method
Modify the underscore code, annotate the original module export statement, use the module.exports = _
force export
//
/Export The underscore object for **node.js**, with
//backwards-compatibility for the old ' require () ' API. I F we ' re
in//The browser, add ' _ ' as a global object.
if (typeof exports!== ' undefined ') {
if (typeof module!== ' undefined ' && module.exports) {
exports = MoD Ule.exports = _;
}
Exports._ = _;
} else {
Root._ = _;
}
* *
module.exports = _;
////AMD registration happens at the "End for Compatibility" with
AMD loaders//' may not
enforce Next-turn sem Antics on modules. Even though general
//Practice for AMD registration are to be anonymous, underscore registers
/as a named module Because, like JQuery, it's a base library that's
/popular enough to being bundled in a third party lib, but don't be p Art of
//an AMD load request. Those cases could generate an error when
//anonymous define () is called outside of a loader request.
if (typeof define = = = ' function ' && define.amd) {
define (' underscore ', [], function () {return
_;
} );
}
*/
Using Underscore.js
Index.js var _ = require ('.. /..
/libs/underscore/underscore.modified.js ');
Get application instance var app = Getapp ();
Page ({onload:function () {//console.log (' onLoad ');
var that = this;
var lines = [];
Lines.push ("_.map" ([1, 2, 3], function (num) {return num * 3;});
Lines.push (_.map ([1, 2, 3], function (num) {return num * 3;});
Lines.push ("var sum = _.reduce ([1, 2, 3], function (memo, NUM) {return memo + num;}, 0);");
Lines.push (_.reduce ([1, 2, 3], function (memo, NUM) {return memo + num;}, 0));
Lines.push ("var even = _.find ([1, 2, 3, 4, 5, 6], function (num) {return num% 2 = 0;}");
Lines.push (_.find ([1, 2, 3, 4, 5, 6], function (num) {return num% 2 = 0;}));
Lines.push ("_.sortby" ([1, 2, 3, 4, 5, 6], function (num) {return math.sin (num);});
Lines.push (_.sortby ([1, 2, 3, 4, 5, 6], function (num) {return math.sin (num);}));
Lines.push ("_.indexof" ([1, 2, 3], 2);
Lines.push (_.indexof ([1, 2, 3], 2)); This.setdata ( {text:lines.join (' \ n ')}) }
})
Summarize
The above is the micro-credit program using the Third-party library underscore.js all the content, I hope that the study or work to bring certain help, if you have questions you can message exchange.