Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!newsfeed.straub-nv.de!uucp.gnuu.de!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <2169875.p79QOsc7RG@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Mon, 16 Jul 2012 21:29:49 +0200 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 8Bit X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 79 NNTP-Posting-Date: 16 Jul 2012 21:29:51 CEST NNTP-Posting-Host: ee864b1a.newsspool4.arcor-online.net X-Trace: DXC=fR7e_5EURKofF8a^:6>b7e4IUK4UH3dlTk?eCF@]BF`?gTc X-Complaints-To: usenet-abuse@arcor.de Xref: csiph.com comp.lang.javascript:15079 Gregor Kofler wrote: > Am 2012-07-16 02:50, perryv@gmail.com meinte: >> […] lately, I've been reading that "document", "window" and other browser >> objects are "host objects", and host objects have their own rules and >> behaviors that depend on the browser and the platform. So it's possible >> that merely testing "document.someFunctionality" can throw an exception >> in some browser on some platform. >> >> So, in order to cover those cases, is it reasonable to do the following? >> >> if(typeof document.someProperty != 'undefined') { >> // Go ahead and use the property >> } >> >> if(typeof document.someFunction == 'function') { >> // Go ahead and invoke "document.someFunction()" >> } >> >> Or is it overkill? > > Quite the contrary. > > Some links for further reading: > > > […] I have since realized not only the usefulness of having a method that can deal with separate object reference and property names instead of a `typeof' result (isMethod), but also of having a separate method for methods of host and native objects (isNativeMethod) each. (jsx.object.isMethodType(), formerly isMethodType(), is still supported but has been marked deprecated accordingly. It may not be supported in future revisions.) Therefore, JSX:object.js contains jsx.object.isMethod()) (formerly isMethod()) since at least revision 123 (2008-05-29), and jsx.object.isHostMethod() (alias for .isMethod()) and jsx.object.isNativeMethod() (wrapper for .isMethod()) since revision 234 (2011-12-12). jsx.object.areNativeMethods(), jsx.object.areHostMethods(), and jsx.object.areMethods(), the latter of which has been been introduced in revision 146 (2009-07-16), are aliases for those methods, respectively. As an extension to the methods in David's library (so to speak), they also support calls of the form if (jsx.object.isHostMethod(window, "document", "write")) (accepts an arbitrary number of arguments) which prevents you from having to write if (typeof window.document != "undefined" && jsx.object.isHostMethod(window.document, "write")) and if (jsx.object.areHostMethods(document, ["open", "write", "close"])) which prevents you from having to write var _isHostMethod = jsx.object.isHostMethod; if (_isHostMethod(document, "open") && _isHostMethod(document, "write") && _isHostMethod(document, "close")) which is not only tedious to write but also inefficient by comparison. See for more. PointedEars -- > If you get a bunch of authors […] that state the same "best practices" > in any programming language, then you can bet who is wrong or right... Not with javascript. Nonsense propagates like wildfire in this field. -- Richard Cornford, comp.lang.javascript, 2011-11-14