underscore.js

JavaScript是按引用传递还是按值传递语言?

我们两清 提交于 2019-12-09 15:11:33
基本类型(数字,字符串等)按值传递,但对象是未知的,因为它们都可以按值传递(如果我们认为保存对象的变量实际上是对对象的引用) )和按引用传递(当我们认为对象的变量包含对象本身时)。 尽管最后并没有什么大不了,但我想知道呈现通过惯例的参数的正确方法是什么。 是否有JavaScript规范的摘录,其中定义了与此相关的语义? #1楼 当我想将一个对象作为参数传递或修改或完全替换时,我发现 Underscore.js库 的 extend方法 非常有用。 function replaceOrModify(aObj) { if (modify) { aObj.setNewValue('foo'); } else { var newObj = new MyObject(); // _.extend(destination, *sources) _.extend(newObj, aObj); } } #2楼 基元按值传递,对象按引用传递。 这与其他语言(如C,Visual Basic或Delphi)完全不同。 我不能说它们如何精确地处理对象和基元,但是我知道Visual Basic和Delphi可以(并且应该)指定它们。 从版本5开始,PHP进行了类似的操作:所有对象都通过引用传递,但是所有原语 都 可以通过引用传递(如果前面带有&符)。 否则,基元将按值传递。 因此,在JavaScript中

how to install underscore.js in my angular application?

半城伤御伤魂 提交于 2019-12-09 10:30:53
问题 I used yo-angular to generate my angularjs template with bootstrap/grunt/bower. I also want to use underscore in the app: npm install underscore --save-dev In the MainCtrl I am calling underscore.js just to see whether it works: angular.module('yomanApp') .controller('MainCtrl', function ($scope) { $scope.awesomeThings = [ 'HTML5 Boilerplate', 'AngularJS', 'AngularJS' ]; _.each([1,2,3],console.log); }); When I run the application with Chrome I get this errmsg in the console: ReferenceError: _

Check if a list is sorted in javascript using underscore.js

烈酒焚心 提交于 2019-12-09 05:28:13
问题 In javascript (underscore) , how do I test whether a list of numbers is already sorted or not? 回答1: You can use _.every to check whether all elements are in order: _.every(arr, function(value, index, array) { // either it is the first element, or otherwise this element should // not be smaller than the previous element. // spec requires string conversion return index === 0 || String(array[index - 1]) <= String(value); }); 回答2: A simple solution would be to simply iterate over the list,

Backbone JS multiple level navigation example

别说谁变了你拦得住时间么 提交于 2019-12-09 04:28:14
问题 I'm trying to construct a solid Backbone JS experiment, where I have a local JSON data file which contains my pages (a project I'm doing has this sort of requirement anyhow). And I've coded this example so I can have endless nested subpages in the pages data. It seems to be working great. But when it comes to the URLs, I'm a little stuck. How do I approach giving this multiple level navigation example totally dynamic URLs? What I mean is, correctly using the url property of the models and

Filter array of objects by object key

南楼画角 提交于 2019-12-09 01:07:09
问题 I have an array of objects in Javascript: var List = [ { employee:'Joe', type:'holiday', }, { employee:'Jerry', type:'seminar', }, { employee:'Joe', type:'shore leave', } ]; I would like to obtain two new arrays of objects; one for the key employee "Joe" and the other for the key employee "Jerry". The objects should keep the same pairs of key/values. I have been trying to get a solution using underscore.js, but it is getting too complicated. Any ideas on how this can be achieved? Thanks in

Sort array containing objects based on another array using _Underscore

只谈情不闲聊 提交于 2019-12-08 21:49:32
问题 I already read the previous questions answered, but it didn't fit with my need. I have an array of objects such as var Widgets = [ [{Id: 'abcdef', post_id: 12345}], [{Id: 'ghijkl', post_id: 45678}], [{Id: 'mnoptq', post_id: 90123}] ]; I have a second array : var sortArray = ['ghijkl', 'mnoptq', 'abcdef']; I need To reorder Widgets with the initial order of elements which appears on sortArray i've succeed to do it this way sortArray.forEach(function(Id) { var found = false; Widgets = Widgets

AngularJS limitTo by last 2 records

别说谁变了你拦得住时间么 提交于 2019-12-08 21:04:02
问题 Can a combination of AngularJS filter, order, or limitTo for ng-repeat mimic the _.last in UnderscoreJS? 回答1: If I'm understanding your question correctly I think this fiddle would do it. For this data: $scope.items = [{sort: 1, name: 'First'}, {sort: 2, name: 'Second'}, {sort: 3, name: 'Third'}, {sort: 4, name:'Last'}]; If you don't want to actually sort the array and just take the last two items of the array as is (like underscore's last) you can try a negative limit (this would show Third,

Using jsfiddle: how can I use underscore.js or backbone.js libraries?

99封情书 提交于 2019-12-08 19:16:40
问题 In jsfiddle is possible to use for example jQuery. But I cannot see any references, for example, to underscore or backbone.js. If I run this demo I get the error: Uncaught ReferenceError: _ is not defined How can I use underscore.js or backbone.js libraries in jsfiddle? 回答1: Add the URL for the required library under the "Add Resources" button on the left hand side. See http://jsfiddle.net/alnitak/BwHxv/ 回答2: I'm using a self-made jsfiddle base with log included in the HTML, so you don't have

changing an array to an Object (upper case) by adding a value using a dot

时光毁灭记忆、已成空白 提交于 2019-12-08 17:40:24
问题 Does adding a property to an array with dot notation change it to an object? var arr = []; arr.something = "test"; is it an array? I don't think so, but underscore.js says it is console.log( _.isArray(arr) ); //true http://jsfiddle.net/wZcyG/ 回答1: If you look at the underscore.js source, you will see that the isArray function is defined as: _.isArray = nativeIsArray || function(obj) { return toString.call(obj) == '[object Array]'; }; The brower's native Array.isArray says it's an array

How to compare each object in an array with each other. When found update the object with a new property

霸气de小男生 提交于 2019-12-08 15:32:08
问题 For example consider the following array with objects. var collection = [{name:'user1',phone:'203'}, {name:'user2',phone:'5050'}, {name:'user1',phone:'203'}] Now I want to compare the each object with each other and give the result as. var newCollection = {name:'user1',phone:'203', rowMatch:'true'}, {name:'user2',phone:'5050', rowMatch:'false'}, {name:'user1',phone:'203',rowMatch:'true'} So, I want the new collecition like this where it compares and updates with new property when object