yui-object.js revision 1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore/**
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * Adds the following Object utilities to the YUI instance
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @class YUI~object
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore */
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore/**
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * Y.Object(o) returns a new object based upon the supplied object.
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @TODO Use native Object.create() when available
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @method Object
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @static
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @param o the supplier object
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @return {object} the new object
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore */
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam MooreY.Object = function(o) {
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore var F = function() {};
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore F.prototype = o;
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore return new F();
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore};
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moorevar O = Y.Object, L = Y.Lang;
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore/**
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * Determines whether or not the property was added
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * to the object instance. Returns false if the property is not present
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * in the object, or was inherited from the prototype.
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore *
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @deprecated Safari 1.x support has been removed, so this is simply a
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * wrapper for the native implementation. Use the native implementation
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * directly instead.
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore *
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @TODO Remove in B1
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore *
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @method Object.owns
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @static
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @param o {any} The object being testing
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @param p {string} the property to look for
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @return {boolean} true if the object has the property on the instance
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore */
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam MooreO.owns = function(o, p) {
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore Y.message('Object.owns is deprecated, use the native method');
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore return (o && o.hasOwnProperty) ? o.hasOwnProperty(p) : false;
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore};
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore/**
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * Returns an array containing the object's keys
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @TODO use native Object.keys() if available
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @method Object.keys
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @static
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @param o an object
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @return {string[]} the keys
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore */
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam MooreO.keys = function(o) {
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore var a=[], i;
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore for (i in o) {
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore if (o.hasOwnProperty(i)) {
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore a.push(i);
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore }
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore }
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore return a;
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore};
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore/**
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * Executes a function on each item. The function
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * receives the value, the key, and the object
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * as paramters (in that order).
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @method Object.each
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @static
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @param o the object to iterate
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @param f {function} the function to execute
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @param c the execution context
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @param proto {boolean} include proto
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @return {YUI} the YUI instance
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore */
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam MooreO.each = function (o, f, c, proto) {
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore var s = c || Y;
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore for (var i in o) {
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore if (proto || o.hasOwnProperty(i)) {
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore f.call(s, o[i], i, o);
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore }
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore }
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore return Y;
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore};