Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.teledata-fn.de!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <67583587.7BbXikoxoM@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Wed, 24 Oct 2012 02:15:24 +0100 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: 133 NNTP-Posting-Date: 24 Oct 2012 03:15:24 CEST NNTP-Posting-Host: f0afe0e5.newsspool3.arcor-online.net X-Trace: DXC=fVL4na]Y3:?_A0jCfgHO6>McF=Q^Z^V384Fo<]lROoR18kFK0JjkNj3FBJ;9E<9gJZ32R\1 X-Complaints-To: usenet-abuse@arcor.de Xref: csiph.com comp.lang.javascript:16829 Cezary Tomczyk wrote: > I have read this article: > http://perfectionkills.com/extending-built-in-native-objects-evil-or-not/ > > At the end there is: > > "[...] It should be now clear that extending native built-ins is > definitely not as risky as messing with host objects. Do it carefully, > follow spec closely, and use your reasonable judgement.[...]" > > In my project I use some of the 3-rd party library. I didn't knew that > this library overwritten many of built-in native methods. Do not use that library then; it is junk. A library should only overwrite built-in methods (that is, the properties that refer to the respective Function objects) when they do _not_ exist, or are not as capable as required (if it is reasonable to use this and no other method). Built-in methods are inevitably faster than user-defined ones because in most of the cases the former are already compiled. Any library that provides an emulation of a built-in method should only do that after it has been determined that the built-in method is unavailable: if (typeof foo.whatever != "function") { foo.whatever = function (…) { … }; } or even if (typeof foo.whatever != "undefined") { foo.whatever = function (…) { … }; } `foo' should not refer to a host object then. And whenever a built-in method is overwritten by a library, it should be done so that the specified way of calling that method is still supported. For example, it has been reasonable (but may be considered obsolete now) to overwrite a not-as-capable Math.max() method: /** * Returns the numeric maximum of all arguments. * * @params : Number * @return {Number} * -Infinity if no arguments have been passed. */ function Math_max () { var result = Number.NEGATIVE_INFINITY; /* NOTE: Array.prototype.slice() was not specified before ES 3 */ for (var i = arguments.length; i--;) { var arg = parseFloat(arguments[i]); if (arg > result) { result = arg; } } return result; } var x; if (typeof Math.max == "function") { x = Math.max(1, 2, 3); } if (typeof x == "undefined" || x != 3) { Math.max = Math_max; } > And even worst, they work different than they should. I mean, results is > not the same as expected. > > So, when I'm doing: > > if (String.prototype.trim) > > and its passed then this means that trim method exists. No, it means that the String prototype object has a `trim' property whose value can be type-converted to `true'. > Is there a way to check if (for example: String.prototype.trim) is a > real built-in method? No (looking for "[native code]" is _not_ reliable; function serialization is *implementation-dependent*), and there should not be a need for it. However, you can increase the probabililty that the property (value) is callable, and you can handle odd cases (this may be part of a user-defined wrapper): if (typeof bar.trim == "function") { try { var foo = bar.trim(); } catch (e) { … } } try-catch might need to be guarded for a maximum of backwards-compatibility: if (typeof bar.trim == "function") { eval("try { var foo = bar.trim(); }" + "catch (e) { … }"); } See also: http://PointedEars.de/es-matrix> 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