(三)underscore.js框架Objects类API学习

keys_.keys(object)

Retrieve all the names of the object‘s properties.

_.keys({one: 1, two: 2, three: 3});
=> ["one", "two", "three"]

values_.values(object)

Return all of the values of the object‘s properties.

_.values({one: 1, two: 2, three: 3});
=> [1, 2, 3]

pairs_.pairs(object)

Convert an object into a list of [key, value] pairs.

_.pairs({one: 1, two: 2, three: 3});
=> [["one", 1], ["two", 2], ["three", 3]]

invert_.invert(object)

Returns a copy of the object where the keys have become the values and the values the keys. For this to work, all of your object‘s values should be unique and string serializable.

_.invert({Moe: "Moses", Larry: "Louis", Curly: "Jerome"});
=> {Moses: "Moe", Louis: "Larry", Jerome: "Curly"};

functions_.functions(object) Alias: methods

Returns a sorted list of the names of every method in an object — that is to say, the name of every function property of the object.

_.functions(_);
=> ["all", "any", "bind", "bindAll", "clone", "compact", "compose" ...

extend_.extend(destination,
*sources)

Copy all of the properties in the source objects over to the destination object, and return the destination object. It‘s in-order, so the last source will override properties of the same name in previous arguments.

_.extend({name: ‘moe‘}, {age: 50});
=> {name: ‘moe‘, age: 50}

说明:extend函数是直接修改destination参数的,通过下面代码很容易证明

var destination = {name: 'moe'};
var source = {age: 50}
_.extend(destination, source);
console.log("extend="+destination.age);//50

pick_.pick(object,
*keys)

Return a copy of the object, filtered to only have values for the whitelisted(白名单) keys (or array of valid keys). Alternatively accepts a predicate indicating which keys to pick.

_.pick({name: ‘moe‘, age: 50, userid: ‘moe1‘}, ‘name‘, ‘age‘);
=> {name: ‘moe‘, age: 50}
_.pick({name: ‘moe‘, age: 50, userid: ‘moe1‘}, function(value, key, object) {
  return _.isNumber(value);
});
=> {age: 50}

omit_.omit(object,
*keys)

Return a copy of the object, filtered to omit the blacklisted(黑名单) keys (or array of keys). Alternatively accepts a predicate indicating which keys to omit.

_.omit({name: ‘moe‘, age: 50, userid: ‘moe1‘}, ‘userid‘);
=> {name: ‘moe‘, age: 50}
_.omit({name: ‘moe‘, age: 50, userid: ‘moe1‘}, function(value, key, object) {
  return _.isNumber(value);
});
=> {name: ‘moe‘, userid: ‘moe1‘}

defaults_.defaults(object,
*defaults)

Fill in undefined properties in object with the
first value present in the following list ofdefaults objects.

var iceCream = {flavor: "chocolate"};
_.defaults(iceCream, {flavor: "vanilla", sprinkles: "lots"});
=> {flavor: "chocolate", sprinkles: "lots"}

说明:这个函数和extend很类似,如果destination和source中属性名没有重复,那么2个函数的功能是完全一致的。

差别在于:当属性名有同名的时候,extend直接用source中的值覆盖掉destination中的值;而defaults则会根据destination中的属性值是否为undefined区别对待。

var iceCream = {flavor: "chocolate",sprinkles:undefined};
_.defaults(iceCream, {flavor: "vanilla", sprinkles: "lots"});
console.log("iceCream=" + iceCream.flavor);//chocolate
console.log("sprinkles=" + iceCream.sprinkles);//lots

clone_.clone(object)

Create a shallow-copied(浅拷贝) clone of the object. Any nested objects or arrays will be copied by reference, not duplicated.

_.clone({name: ‘moe‘});
=> {name: ‘moe‘};

tap_.tap(object,
interceptor)

Invokes interceptor with the object, and then returns object. The primary purpose of this method is to "tap into" a method chain, in order to perform operations on intermediate results within the chain.

_.chain([1,2,3,200])
  .filter(function(num) { return num % 2 == 0; })
  .tap(alert)
  .map(function(num) { return num * num })
  .value();
=> // [2, 200] (alerted)
=> [4, 40000]

has_.has(object,
key)

Does the object contain the given key? Identical to object.hasOwnProperty(key),
but uses a safe reference to the hasOwnProperty function, in
case it‘s been overridden accidentally.

_.has({a: 1, b: 2, c: 3}, "b");
=> true

property_.property(key)

Returns a function that will itself return the key property of
any passed-in object.

var moe = {name: ‘moe‘};
‘moe‘ === _.property(‘name‘)(moe);
=> true

matches_.matches(attrs)

Returns a predicate function that will tell you if a passed in object contains all of the key/value properties present in attrs.

var ready = _.matches({selected: true, visible: true});
var readyToGoList = _.filter(list, ready);

isEqual_.isEqual(object,
other)

Performs an optimized deep comparison between the two objects, to determine if they should be considered equal.

var moe   = {name: ‘moe‘, luckyNumbers: [13, 27, 34]};
var clone = {name: ‘moe‘, luckyNumbers: [13, 27, 34]};
moe == clone;
=> false
_.isEqual(moe, clone);
=> true

isEmpty_.isEmpty(object)

Returns true if an enumerable object contains no values (no enumerable own-properties). For strings and array-like objects _.isEmpty checks
if the length property is 0.

_.isEmpty([1, 2, 3]);
=> false
_.isEmpty({});
=> true

isElement_.isElement(object)

Returns true if object is a DOM element.

_.isElement(jQuery(‘body‘)[0]);
=> true

isArray_.isArray(object)

Returns true if object is an Array.

(function(){ return _.isArray(arguments); })();
=> false
_.isArray([1,2,3]);
=> true

isObject_.isObject(value)

Returns true if value is an Object. Note that JavaScript arrays and functions are objects, while (normal) strings and numbers are not.

_.isObject({});
=> true
_.isObject(1);
=> false

isArguments_.isArguments(object)

Returns true if object is an Arguments object.

(function(){ return _.isArguments(arguments); })(1, 2, 3);
=> true
_.isArguments([1,2,3]);
=> false

isFunction_.isFunction(object)

Returns true if object is a Function.

_.isFunction(alert);
=> true

isString_.isString(object)

Returns true if object is a String.

_.isString("moe");
=> true

isNumber_.isNumber(object)

Returns true if object is a Number (including NaN).

_.isNumber(8.4 * 5);
=> true

isFinite_.isFinite(object)

Returns true if object is a finite Number.

_.isFinite(-101);
=> true

_.isFinite(-Infinity);
=> false

isBoolean_.isBoolean(object)

Returns true if object is either true or false.

_.isBoolean(null);
=> false

isDate_.isDate(object)

Returns true if object is a Date.

_.isDate(new Date());
=> true

isRegExp_.isRegExp(object)

Returns true if object is a RegExp.

_.isRegExp(/moe/);
=> true

isNaN_.isNaN(object)

Returns true if object is NaN.

Note: this is not the same as the native isNaN function, which will also return true for many other not-number values, such as undefined.

_.isNaN(NaN);
=> true
isNaN(undefined);
=> true
_.isNaN(undefined);
=> false

isNull_.isNull(object)

Returns true if the value of object is null.

_.isNull(null);
=> true
_.isNull(undefined);
=> false

isUndefined_.isUndefined(value)

Returns true if value is undefined.

_.isUndefined(window.missingVariable);
=> true
时间: 2024-10-12 03:19:29

(三)underscore.js框架Objects类API学习的相关文章

(二)underscore.js框架Utility类API学习以及模型template的详细介绍

本文介绍的是underscore.js提供的Utility Functions. noConflict_.noConflict() Give control of the "_" variable back to its previous owner. Returns a reference to theUnderscore object. 这个函数主要是为了解决underscore.js与其他框架或js名字冲突的问题.我们知道:underscore.js使用全局对象_,如果在unde

java.time包常用类API学习记录

Java8出来已那么多年了,java.time包之前一直没有使用过,最近正好有用到,在此做个记录. 上图列出了java.time包下的类,接下来我们详细看下其中每个类的用法. Clock:获取到当前时间点,包含时区信息,该类是抽象类,其实现类由其内部类实现,也可以自定义其实现类. Clock方法描述: getZone():获取创建日期时间的时区: withZone(ZoneId zone):返回一个指定时区clock副本: instant():返回instant实例: millis():获取当前

(一)underscore入门和数组类工具API学习

underscore.js是一个JS框架,在原生javascript基础上提供了很多有用的工具API.apache提供了commons-lang.commons-io.commons-collections等jar包提供很多java语言常用的工具方法,underscore.js功能与之类似.经常开发JS代码的都知道,JS原生的Array.String等内置的API很少,不能满足实际开发过程中国的需要.所以引入一些工具库很有必要,避免我们重复的写一些本来应该公用的方法. 1.学习资料 unders

underscore.js学习笔记

最近项目需要,开始学习underscore.js,在css88网站上看到有中文API,边看边记录自己能理解的笔记: 以下内容,函数以及部分解释抄自css88愚人码头中文API 内容还在整理中,发出部分 /** *@方法参数解释 *iterator 为迭代器,该函数会获取前面数组或者对象的值作为参数传入,并且一个个进行处理 * predicate 为真值检测,该函数会将前面数组或者对象的值作为参数传入,并根据函数内容进行判断,如果能匹配上则返回true,否则返回false * @函数参数解释 *

JAVA学习第三十四课 (经常使用对象API)—List集合及其子类特点

整个集合框架中最经常使用的就是List(列表)和Set(集) 一.List集合 && Set的特点 Collection的子接口: 1.List:有序(存入和取出的顺序一致),元素都有索引且能够反复    API文档解释:有序的 collection(也称为序列).此接口的用户能够对列表中每一个元素的插入位置进行精确地控制.用户能够依据元素的整数索引(在列表中的位置)訪问元素,并搜索列表中的元素. 2.Set:元素不能反复,无序,有可能会有序    API文档解释:一个不包括反复元素的 c

潜移默化学会WPF--Command(命令)学习(三) - AYUI框架 - 博客园

原文:潜移默化学会WPF--Command(命令)学习(三) - AYUI框架 - 博客园 3.修炼 3.1 自定义命令 涉及到的一些概念,例如 InputGestureCollection这个集合,路由命令(RoutedUICommand)等我们不用太紧张,潜移默化学会他们的用法,Ok,学习吧 先看代码,先看后台 using System.Collections.Generic;using System.Text;using System.Windows;using System.Window

node.js框架StrongLoop学习笔记(一)

node.js框架StrongLoop学习笔记(一) 本人在用node.js做手机后台,查找框架发现StrongLoop挺适合,可是却发现没有中文教程,于是在自己学习时,做一下笔记,以方便其他像我一样的人参考(本人的英语水平非常差,只能一点点试着做,并记录下来,如果大家发现问题,请通知我好更正,谢谢了!).所有操作都是在CentOS7-x64,Node.js 0.12.2下完成的. nodejs框架StrongLoop学习笔记一 安装StrongLoop 创建项目 安装数据库驱动 配置数据库连接

跟王老师学枚举(三):枚举类API

跟王老师学枚举(三):枚举API 主讲教师:王少华   QQ群号:483773664 一.枚举类API Java中声明的枚举类,均是java.lang.Enum类的孩子,它继承了Enum类的所有方法.常用方法: name():返回此枚举常量的名称 ordinal():返回枚举常量的序数(它在枚举声明中的位置,其中初始常量序数为零 valueof(Class enumClass, String name):返回带指定名称的指定枚举类型的枚举常量 valueof(String name):返回带指定

【PMP】Head First PMP 学习笔记 第三章 过程框架

第三章 过程框架 项目中完成的所有工作都由过程构成. 项目中的完成的所有工作都有一个模式(pattern).先计划,再去做.工作时,总是对项目与原先的计划进行比较.如果开始偏离计划,就要由你做出矫正,让一切重新走上正轨.过程框架--过程租和知识领域--正式这一切顺利完成的关键. 分阶段管理 分阶段,项目的每个阶段(phase)都会经过5个过程租,从启动到收尾,项目的多个阶段就会存在各种关联关系 顺序关系(sequenital relationship).多个阶段相继发生并不存在重叠,每个阶段在前