(3) underscore. js framework Objects API Learning

Source: Internet
Author: User
Tags hasownproperty

(3) underscore. js framework Objects API Learning


Keys_.keys(object)
Retrieve all the names ofObject'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[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 shoshould 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 inSourceObjects over toDestinationObject, and returnDestinationObject. 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}
Note: The extend function directly modifies the destination parameter. It is easily proved by the following code:

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 (whitelist) 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 (blacklist) 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'}

Ults_.defaults(object, *defaults)
Fill inUndefinedProperties inObjectWith the first value present in the following listUltsObjects.

var iceCream = {flavor: "chocolate"};_.defaults(iceCream, {flavor: "vanilla", sprinkles: "lots"});=> {flavor: "chocolate", sprinkles: "lots"}
Note: This function is similar to extend. If the attribute names in destination and source are not repeated, the functions of the two functions are completely consistent.

The difference is that when the attribute name has the same name, extend directly overwrites the value in destination with the value in source, and ults treats it differently based on whether the attribute value in destination is undefined.

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

Clone_.clone(object)
Create a shallow-copied (shallow copy) 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? IdenticalObject. hasOwnProperty (key), But uses a safe reference toHasOwnPropertyFunction, 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 returnKeyProperty 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 shocould 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)
ReturnsTrueIf an enumerable object contains no values (no enumerable own-properties). For strings and array-like objects_. IsEmptyChecks if the length property is 0.

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

IsElement_.isElement(object)
ReturnsTrueIf object is a DOM element.

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

IsArray_.isArray(object)
ReturnsTrueIf object is an Array.

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

IsObject_.isObject(value)
ReturnsTrueIf 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)
ReturnsTrueIf object is an Arguments object.

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

IsFunction_.isFunction(object)
ReturnsTrueIf object is a Function.

_.isFunction(alert);=> true

IsString_.isString(object)
ReturnsTrueIf object is a String.

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

IsNumber_.isNumber(object)
ReturnsTrueIf object is a Number (includingNaN).

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

IsFinite_.isFinite(object)
ReturnsTrueIf object is a finite Number.

_.isFinite(-101);=> true_.isFinite(-Infinity);=> false

IsBoolean_.isBoolean(object)
ReturnsTrueIf object is eitherTrueOrFalse.

_.isBoolean(null);=> false

IsDate_.isDate(object)
ReturnsTrueIf object is a Date.

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

IsRegExp_.isRegExp(object)
ReturnsTrueIf object is a RegExp.

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

IsNaN_.isNaN(object)
ReturnsTrueIf object isNaN.
Note: this is not the same as the nativeIsNaNFunction, which will also return true for your other not-number values, suchUndefined.

_.isNaN(NaN);=> trueisNaN(undefined);=> true_.isNaN(undefined);=> false

IsNull_.isNull(object)
ReturnsTrueIf the value of object isNull.

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

IsUndefined_.isUndefined(value)
ReturnsTrueIf value isUndefined.

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



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.