摘要:是一个工具库,它提供了一整套函数式编程的实用功能,但是没有扩展任何内置对象。微信小程序无法直接使用进行调用。通过测试,微信小程序运行环境并没有定义获取应用实例解决方法修改代码,注释原有模块导出语句,使用强制导出使用获取应用实例其他完整代码
Underscore.js 是一个 JavaScript 工具库,它提供了一整套函数式编程的实用功能,但是没有扩展任何 JavaScript 内置对象。Underscore 提供了100多个函数,包括常用的:map、filter、invoke — 当然还有更多专业的辅助函数,如:函数绑定、JavaScript 模板功能、创建快速索引、强类型相等测试等等。 微信小程序无法直接使用require( "underscore.js" )进行调用。
微信小程序模块化机制微信小程序运行环境支持CommoJS模块化,通过module.exports暴露对象,通过require来获取对象。
微信小程序Quick Start utils/util.js
</>复制代码
function formatTime(date) {
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate()
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(":")
}
function formatNumber(n) {
n = n.toString()
return n[1] ? n : "0" + n
}
module.exports = {
formatTime: 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))
})
})
}
})
原因分析
Underscore CommonJs模块导出代码如下:
</>复制代码
// 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 = module.exports = _;
}
exports._ = _;
} else {
root._ = _;
}
exports、module必须都有定义,才能导出。通过测试,微信小程序运行环境exports、module并没有定义
</>复制代码
//index.js
//获取应用实例
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);
}
})
解决方法
修改Underscore代码,注释原有模块导出语句,使用module.exports = _ 强制导出
</>复制代码
/*
// 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 = module.exports = _;
}
exports._ = _;
} else {
root._ = _;
}
*/
module.exports = _;
</>复制代码
/*
// AMD registration happens at the end for compatibility with AMD loaders
// that may not enforce next-turn semantics on modules. Even though general
// practice for AMD registration is to be anonymous, underscore registers
// as a named module because, like jQuery, it is a base library that is
// popular enough to be bundled in a third party lib, but not be part of
// an AMD load request. Those cases could generate an error when an
// anonymous define() is called outside of a loader request.
if (typeof define === "function" && define.amd) {
define("underscore", [], function() {
return _;
});
}
*/
使用Underscore.js
</>复制代码
//index.js
var _ = require( "../../libs/underscore/underscore.modified.js" );
//获取应用实例
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( "
" )
})
}
})
其他
完整代码 https://github.com/guyoung/Gy...
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/80501.html
摘要:以微信小程序调试时代码为例兼容兼容微信小程序运行的代码与模块规范基本符合。使用第三方模块微信小程序运行环境没有定义,无法通过导入模块,需要对第三方模块强制导出后才能正常导入。 JavaScript模块规范 在任何一个大型应用中模块化是很常见的,与一些更传统的编程语言不同的是,JavaScript (ECMA-262版本)还不支持原生的模块化。 Javascript社区做了很多努力,在现...
摘要:昨日月,腾讯终于发布了没有,无需申请也可以进行微信小程序开发的视频教程了,我在在第一时间尝试并发布了这个小视频教程,入门足够了各位免费拿去,慢慢享用链接密码也可以添加微信小程序开发者交流群,只欢迎对微信小程序开发有兴趣的朋友,其他勿加,感谢 昨日(9月23),腾讯终于发布了没有APPid,无需申请也可以进行微信小程序开发的视频教程了,我在在第一时间尝试并发布了这7个小视频教程,入门足够...
摘要:微信小程序应用号开发资源汇总文档工具教程代码插件组件文档从搭建一个微信小程序开始小程序开发文档小程序设计指南工具小程序开发者工具官方支持微信小程序实时预览的支持的微信小程序组件化开发框架转在线工具小程序云端增强社区微信小程序 微信(小程序or应用号)开发资源汇总-文档-工具-教程-代码-插件-组件 文档 从搭建一个微信小程序开始 小程序开发文档 小程序设计指南 工具 小程序开发者...
阅读 1218·2021-11-24 10:43
阅读 3295·2021-11-22 09:34
阅读 3624·2021-10-08 10:04
阅读 4023·2021-09-23 11:58
阅读 3176·2019-08-30 15:44
阅读 551·2019-08-30 13:01
阅读 1240·2019-08-28 18:07
阅读 1513·2019-08-26 13:42