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

_.keys(object)
Retrieve all the names of the object‘s properties.

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

_.values(object)
Return all of the values of the object’s properties.

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

_.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(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(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(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}
var destination = {name: 'moe'};
var source = {age: 50}
_.extend(destination, source);
console.log("extend="+destination.age);//50

_.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(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(object,
*defaults)

Fill in undefined properties in object with the first value present in the following list of defaults 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(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(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(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(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(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(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(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(object)
Returns true if object is a DOM element.

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

_.isArray(object)
Returns true if object is an Array.

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

_.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(object)
Returns true if object is an Arguments object.

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

_.isFunction(object)
Returns true if object is a Function.

_.isFunction(alert);
=> true

_.isString(object)
Returns true if object is a String.

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

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

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

_.isFinite(object)
Returns true if object is a finite Number.

_.isFinite(-101);
=> true

_.isFinite(-Infinity);
=> false

_.isBoolean(object)
Returns true if object is either true or false.

_.isBoolean(null);
=> false

_.isDate(object)
Returns true if object is a Date.

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

_.isRegExp(object)
Returns true if object is a RegExp.

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

_.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(object)
Returns true if the value of object is null.

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

_.isUndefined(value)
Returns true if value is undefined.

_.isUndefined(window.missingVariable);
=> true

Original: https://www.cnblogs.com/cxchanpin/p/7403721.html
Author: cxchanpin
Title: (三)underscore.js框架Objects类API学习

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/551473/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球