jQuery - Utility methods and objects

jquery

What is the purpose of the $.browser object?

It helps detect the browser agent and version number for when browser detection is the only choice. Ideally we should use feature detection.

What are the properties of the $.browser object?

Property Description
msie Set to true if the user agent header identifies the browser as Microsoft Internet Explorer.
mozilla Set to true if the user agent header identifies the browser as any Mozilla-based browser.
safari Set to true if the user agent header identifies the browser as Safari or any Safari-based browser such as OmniWeb
opera Set to true if the user agent header identifies the browser as Opera
version Set to the version number of the rendering engine

How can we detect the box model being used?

Use the $.boxModel object or property. This is set to true if the page is using the W3C standard box model, and is set to false if the page is using the Internet Explorer box model (or quirksmode).

How can we filter an array without directly using a loop?

Use the .grep method

How can we iterate over an array with out directly using a loop?

Use the .each method

How can we map an array?

Use the .map method.

How can we determine if an array contains a particular element?

Use the $.inArray method:

var index = $.inArray(2,[1,2,3,4,5]);

What is the purpose of the $.makeArray method?

var images = document.getElementsByTagName('img');

In the above code, images is a NodeList object that contains all the images on the page. It is an array-like object but it is not really an array. Dealing with a NodeList is a bit of a pain, so converting it into a JavaScript array makes thing a bit nicer:

images = $.makeArray(images);

What is the purpose of the $.unique utility method?

$.unique(array)

Given an array of DOM elements, returns an array of the unique elements in the original array.

What is the syntax for $.extend utility method?

$.extend(target, source1, source2, ... sourceN)

The $.extend utility method extends the object passed as 'target' with the properties of the remaining passed objects.

  1. target: (Object). The object whose properties are augmented with the properties of the source objects. This object is directly modified with the new properties before being returned as the value of the function. Any properties with the same name as the properties in any of the source objects are overwritten with the values from the source objects.

How can we merge multiple objects into one object?

Use the $.extend utility method:

$.extend(target, source1, source2, ... sourceN)

The $.extend utility method extends the object passed as 'target' with the properties of the remaining passed objects.

  1. target: (Object). The object whose properties are augmented with the properties of the source objects. This object is directly modified with the new properties before being returned as the value of the function. Any properties with the same name as the properties in any of the source objects are overwritten with the values from the source objects.
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License