/* Copyright (c) 2004-2006, The Dojo Foundation All Rights Reserved. Licensed under the Academic Free License version 2.1 or above OR the modified BSD license. For more information on Dojo licensing, see: http://dojotoolkit.org/community/licensing.shtml */ dojo.provide("dojo.lang.common"); dojo.require("dojo.lang"); /* * Adds the given properties/methods to the specified object */ dojo.lang._mixin = function(obj, props){ var tobj = {}; for(var x in props){ // the "tobj" condition avoid copying properties in "props" // inherited from Object.prototype. For example, if obj has a custom // toString() method, don't overwrite it with the toString() method // that props inherited from Object.protoype if(typeof tobj[x] == "undefined" || tobj[x] != props[x]) { obj[x] = props[x]; } } // IE doesn't recognize custom toStrings in for..in if(dojo.render.html.ie && dojo.lang.isFunction(props["toString"]) && props["toString"] != obj["toString"]) { obj.toString = props.toString; } return obj; } /* * Adds the properties/methods of argument Objects to obj */ dojo.lang.mixin = function(obj, props /*, props, ..., props */){ for(var i=1, l=arguments.length; i -1; // return: boolean } /** * Partial implmentation of is* functions from * http://www.crockford.com/javascript/recommend.html * NOTE: some of these may not be the best thing to use in all situations * as they aren't part of core JS and therefore can't work in every case. * See WARNING messages inline for tips. * * The following is* functions are fairly "safe" */ dojo.lang.isObject = function(wh){ if(typeof wh == "undefined"){ return false; } return (typeof wh == "object" || wh === null || dojo.lang.isArray(wh) || dojo.lang.isFunction(wh)); } dojo.lang.isArray = function(wh){ return (wh instanceof Array || typeof wh == "array"); } dojo.lang.isArrayLike = function(wh){ if(dojo.lang.isString(wh)){ return false; } if(dojo.lang.isFunction(wh)){ return false; } // keeps out built-in ctors (Number, String, ...) which have length properties if(dojo.lang.isArray(wh)){ return true; } if(typeof wh != "undefined" && wh && dojo.lang.isNumber(wh.length) && isFinite(wh.length)){ return true; } return false; } dojo.lang.isFunction = function(wh){ if(!wh){ return false; } return (wh instanceof Function || typeof wh == "function"); } dojo.lang.isString = function(wh){ return (wh instanceof String || typeof wh == "string"); } dojo.lang.isAlien = function(wh){ if(!wh){ return false; } return !dojo.lang.isFunction() && /\{\s*\[native code\]\s*\}/.test(String(wh)); } dojo.lang.isBoolean = function(wh){ return (wh instanceof Boolean || typeof wh == "boolean"); } /** * The following is***() functions are somewhat "unsafe". Fortunately, * there are workarounds the the language provides and are mentioned * in the WARNING messages. * * WARNING: In most cases, isNaN(wh) is sufficient to determine whether or not * something is a number or can be used as such. For example, a number or string * can be used interchangably when accessing array items (arr["1"] is the same as * arr[1]) and isNaN will return false for both values ("1" and 1). Should you * use isNumber("1"), that will return false, which is generally not too useful. * Also, isNumber(NaN) returns true, again, this isn't generally useful, but there * are corner cases (like when you want to make sure that two things are really * the same type of thing). That is really where isNumber "shines". * * RECOMMENDATION: Use isNaN(wh) when possible */ dojo.lang.isNumber = function(wh){ return (wh instanceof Number || typeof wh == "number"); } /** * WARNING: In some cases, isUndefined will not behave as you * might expect. If you do isUndefined(foo) and there is no earlier * reference to foo, an error will be thrown before isUndefined is * called. It behaves correctly if you scope yor object first, i.e. * isUndefined(foo.bar) where foo is an object and bar isn't a * property of the object. * * RECOMMENDATION: Use `typeof foo == "undefined"` when possible * * FIXME: Should isUndefined go away since it is error prone? */ dojo.lang.isUndefined = function(wh){ return ((wh == undefined)&&(typeof wh == "undefined")); } // end Crockford functions