underscore

Javascript模块化编程(三):require.js的用法

本秂侑毒 提交于 2019-12-07 10:10:47
Javascript模块化编程(三):require.js的用法 这个系列的 第一部分 和 第二部分 ,介绍了Javascript模块原型和理论概念,今天介绍如何将它们用于实战。 我采用的是一个非常流行的库 require.js 。 一、为什么要用require.js? 最早的时候,所有Javascript代码都写在一个文件里面,只要加载这一个文件就够了。后来,代码越来越多,一个文件不够了,必须分成多个文件,依次加载。下面的网页代码,相信很多人都见过。   <script src="1.js"></script>   <script src="2.js"></script>   <script src="3.js"></script>   <script src="4.js"></script>   <script src="5.js"></script>   <script src="6.js"></script> 这段代码依次加载多个js文件。 这样的写法有很大的缺点。首先,加载的时候,浏览器会停止网页渲染,加载文件越多,网页失去响应的时间就会越长;其次,由于js文件之间存在依赖关系,因此必须严格保证加载顺序(比如上例的1.js要在2.js的前面),依赖性最大的模块一定要放到最后加载,当依赖关系很复杂的时候,代码的编写和维护都会变得困难。 require.js的诞生

模块化Javascript编程基础

房东的猫 提交于 2019-12-07 10:10:36
(一) 随着网站逐渐变成" 互联网应用程序 ",嵌入网页的Javascript代码越来越庞大,越来越复杂。 网页越来越像桌面程序,需要一个团队分工协作、进度管理、单元测试等等......开发者不得不使用软件工程的方法,管理网页的业务逻辑。 Javascript模块化编程,已经成为一个迫切的需求。理想情况下,开发者只需要实现核心的业务逻辑,其他都可以加载别人已经写好的模块。 但是,Javascript不是一种模块化编程语言,它不支持" 类 "(class),更遑论"模块"(module)了。(正在制定中的 ECMAScript标准 第六版,将正式支持"类"和"模块",但还需要很长时间才能投入实用。) Javascript社区做了很多努力,在现有的运行环境中,实现"模块"的效果。本文总结了当前"Javascript模块化编程"的最佳实践,说明如何投入实用。虽然这不是初级教程,但是只要稍稍了解Javascript的基本语法,就能看懂。 一、原始写法 模块就是实现特定功能的一组方法。 只要把不同的函数(以及记录状态的变量)简单地放在一起,就算是一个模块。   function m1(){     //...   }   function m2(){     //...   } 上面的函数m1()和m2(),组成一个模块。使用的时候,直接调用就行了。 这种做法的缺点很明显:"污染"了全局变量

使用 underscore.template 扩展 jQuery

∥☆過路亽.° 提交于 2019-12-05 10:42:49
最近在接触了一些前端模板框架后,突然觉得以前在 AJAX 操作中拼接组装大量 DOM 对象是多么笨。在了解了 underscore 库的 template 方法后,准备在公司项目中来使用它,但是公司还在用 jQuery,因此准备拿它扩展 jQuery。 先上代码,熟悉 underscore 的同学可以略过了。 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 * 使用 underscore.template 扩展 jquery * @author nekolr */;(function ($) { var escapes = { "'": "'", '\': '\', 'r': 'r', 'n': 'n', 'u2028': 'u2028', 'u2029': 'u2029' }, escapeMap = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''', '`': '`' }; var escapeChar = function(match) { return '\' +

artifactory npm repository unable to install

匿名 (未验证) 提交于 2019-12-03 09:06:55
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: After I installed Artifactory V5.5.1 and with one-click npm setup. I added it to my npm registry, and use npm install something, gives me Cannot read property 'replace' of null . I checked the ~/.npm/npm/underscore/ directory, it was all empty (artifactory and npm is running behind nginx, and given an internal domain) I've tried npm v3 and v4, with both node v6 and v8 following are the logs of running npm i underscore 0 info it worked if it ends with ok 1 verbose cli [ '/Users/Joesonw/.nvm/versions/node/v8.4.0/bin/node', 1 verbose cli '

How to use underscore's “intersection” on objects?

匿名 (未验证) 提交于 2019-12-03 09:02:45
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: _ . intersection ([], []) only works with primitive types, right? It doesn't work with objects. How can I make it work with objects (maybe by checking the "Id" field)? var a = [ { 'id' : 1 , 'name' : 'jake' }, { 'id' : 4 , 'name' : 'jenny' } ] var b = [ { 'id' : 1 , 'name' : 'jake' }, { 'id' : 9 , 'name' : 'nick' } ] In this example, the result should be: _ . intersection ( a , b ); [ {'id': 1, 'name': 'jake' } ]; 回答1: You can create another function based on underscore's function. You only have to change one line of code from the

Scala underscore - ERROR: missing parameter type for expanded function

匿名 (未验证) 提交于 2019-12-03 08:56:10
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I know there have been quite a few questions on this, but I've created a simple example that I thought should work,but still does not and I'm not sure I understand why val myStrings = new Array[String](3) // do some string initialization // this works myStrings.foreach(println(_)) // ERROR: missing parameter type for expanded function myStrings.foreach(println(_.toString)) Can someone explain why the second statement does not compile? 回答1: It expands to: myStrings.foreach(println(x => x.toString)) You want: myStrings.foreach(x => println(x

If I use `module(“somelib”)` in typescript, it can&#039;t be running in browser

匿名 (未验证) 提交于 2019-12-03 08:51:18
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to use typescript with angularjs in the client-side. I found if I use external modules, the generated js won't be run in browser. controllers.ts /// <reference path="./libs/underscore.d.ts"/> import _ = module("underscore"); module test { export class Ctrl { constructor($scope:any) { $scope.name = "Freewind"; _.each($scope.name, function(item) {}); } } } The generated js will be: var _ = require("underscore") var test; (function (test) { var Ctrl = (function () { function Ctrl($scope) { $scope.name = "Freewind"; _.each($scope.name

Reserved names in the global namespace

匿名 (未验证) 提交于 2019-12-03 08:46:08
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Arising from my answer to Dynamic array of objects in C++ and as a follow up to What are the rules about using an underscore in a C++ identifier? : apparently, names beginning with _ followed by an uppercase letter are reserved in the global namespace. 17.4.3.2.1 Global names [ lib.global.names ] Certain sets of names and function signatures are always reserved to the implementation: Each name that contains a double underscore ( __ ) or begins with an underscore followed by an uppercase letter (2.11) is reserved to the implementation for any

How to use Underscore.js filter with an object?

匿名 (未验证) 提交于 2019-12-03 08:42:37
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have an object like so: > Object > Rett@site.com: Array[100] > pel4@gmail.com: Array[4] > 0 id : 132 selected : true > 1 id : 51 selected : false etc.. How can I use the underscore _.filter() to return back only the items where selected === true? I've never had the need to go down to layers with _.filter() . Something like var stuff = _.filter(me.collections, function(item) { return item[0].selected === true; }); Thank you 回答1: If you want to pull all array elements from any e-mail address where selected is true , you can iterate like so:

Unable to derive module descriptor for auto generated module names in Java 9?

匿名 (未验证) 提交于 2019-12-03 08:33:39
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: My project depends on Netty Epoll transport. Here is dependency: io.netty netty-transport-native-epoll ${netty.version} ${epoll.os} The auto-generated module name for this dependency is: netty.transport.native.epoll And as the native keyword is reserved in Java 9 I can't add this module as a dependency to my project: module core { requires netty.transport.native.epoll; } Due to: module not found: netty.transport. Additionally the jar tool --describe-module reports the following: Are there any workarounds? (except "release correct netty