欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序使用第三方庫Underscore.js步驟詳解

 更新時間:2016年09月27日 10:56:22   投稿:daisy  
大家都知道Underscore.js是一個 JavaScript 工具庫,它提供了一整套函數(shù)式編程的實(shí)用功能,但是沒有擴(kuò)展任何 JavaScript 內(nèi)置對象。那么這篇文章我們就來學(xué)習(xí)下微信小程序如何使用第三方庫Underscore.js,有需要的可以參考學(xué)習(xí)。

前言

Underscore.js是一個很精干的庫,壓縮后只有4KB。Underscore 提供了100多個函數(shù),包括常用的:map、filter、invoke — 當(dāng)然還有更多專業(yè)的輔助函數(shù),如:函數(shù)綁定、JavaScript 模板功能、創(chuàng)建快速索引、強(qiáng)類型相等測試等等。彌補(bǔ)了標(biāo)準(zhǔn)庫的不足,大大方便了JavaScript的編程。

微信小程序無法直接使用require( 'underscore.js' )進(jìn)行調(diào)用。

微信小程序模塊化機(jī)制

微信小程序運(yùn)行環(huán)境支持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模塊導(dǎo)出代碼如下:

// 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必須都有定義,才能導(dǎo)出。通過測試,微信小程序運(yùn)行環(huán)境exports、module并沒有定義

//index.js

//獲取應(yīng)用實(shí)例
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代碼,注釋原有模塊導(dǎo)出語句,使用module.exports = _ 強(qiáng)制導(dǎo)出

 /*
 // 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' );

//獲取應(yīng)用實(shí)例
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' )
 })
 }
})

總結(jié)

以上就是微信小程序使用第三方庫Underscore.js的全部內(nèi)容,希望對大家的學(xué)習(xí)或者工作帶來一定的幫助,如果有疑問大家可以留言交流。

相關(guān)文章

  • JS實(shí)現(xiàn)中文漢字按拼音排序的方法

    JS實(shí)現(xiàn)中文漢字按拼音排序的方法

    這篇文章主要介紹了JS實(shí)現(xiàn)中文漢字按拼音排序的方法,涉及javascript針對中文字符串的轉(zhuǎn)換、遍歷、排序等相關(guān)操作技巧,需要的朋友可以參考下
    2017-10-10
  • 詳解template標(biāo)簽用法(含vue中的用法總結(jié))

    詳解template標(biāo)簽用法(含vue中的用法總結(jié))

    這篇文章主要介紹了template標(biāo)簽用法(含vue中的用法總結(jié)),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2021-01-01
  • 利用JavaScript實(shí)現(xiàn)簡單3D開關(guān)書本模型

    利用JavaScript實(shí)現(xiàn)簡單3D開關(guān)書本模型

    這篇文章主要為大家詳細(xì)介紹了如何利用JavaScript實(shí)現(xiàn)簡單3D開關(guān)書本模型的效果,文中的實(shí)現(xiàn)代碼講解的非常詳細(xì),具有一定參考價值,感興趣的同學(xué)可以動手嘗試一下
    2023-11-11
  • js鼠標(biāo)點(diǎn)擊圖片切換效果實(shí)現(xiàn)代碼

    js鼠標(biāo)點(diǎn)擊圖片切換效果實(shí)現(xiàn)代碼

    這篇文章為大家分享了js鼠標(biāo)點(diǎn)擊圖片切換效果實(shí)現(xiàn)代碼,特別炫酷的效果,具有一定的參考價值,推薦給大家,感興趣的小伙伴們可以參考一下
    2015-11-11
  • js實(shí)現(xiàn)圓形顯示鼠標(biāo)單擊位置

    js實(shí)現(xiàn)圓形顯示鼠標(biāo)單擊位置

    這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)圓形顯示鼠標(biāo)單擊位置,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • JS逆向之加密參數(shù)定位

    JS逆向之加密參數(shù)定位

    越來越多的網(wǎng)站進(jìn)行數(shù)據(jù)傳輸時不使用明文傳輸,本文主要介紹了JS逆向之加密參數(shù)定位,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-01-01
  • javascript中日期函數(shù)new Date()的瀏覽器兼容性問題

    javascript中日期函數(shù)new Date()的瀏覽器兼容性問題

    這篇文章主要介紹了javascript中日期函數(shù)new Date()的瀏覽器兼容性問題,需要的朋友可以參考下
    2015-09-09
  • 前端深入理解Typescript泛型概念

    前端深入理解Typescript泛型概念

    這篇文章主要介紹了前端深入理解Typescript泛型概念,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • JS實(shí)現(xiàn)為動態(tài)創(chuàng)建的元素添加事件操作示例

    JS實(shí)現(xiàn)為動態(tài)創(chuàng)建的元素添加事件操作示例

    這篇文章主要介紹了JS實(shí)現(xiàn)為動態(tài)創(chuàng)建的元素添加事件操作,涉及javascript頁面元素動態(tài)添加及事件響應(yīng)相關(guān)操作技巧,需要的朋友可以參考下
    2018-03-03
  • JavaScript實(shí)現(xiàn)圖片輪播特效

    JavaScript實(shí)現(xiàn)圖片輪播特效

    這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)圖片輪播特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-10-10

最新評論