Path: csiph.com!usenet.pasdenom.info!aioe.org!.POSTED!not-for-mail From: Cezary Tomczyk Newsgroups: comp.lang.javascript Subject: Re: Built-in native method Date: Sun, 21 Oct 2012 18:46:27 +0200 Organization: Aioe.org NNTP Server Lines: 84 Message-ID: References: NNTP-Posting-Host: RMrsF+s1qSnxq5Qkkqjnpw.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20121010 Thunderbird/16.0.1 X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.javascript:16765 W dniu 2012-10-21 08:44, JJ pisze: > Cezary Tomczyk wrote: >> So, when I'm doing: >> >> if (String.prototype.trim) >> >> and its passed then this means that trim method exists. The only problem >> is that I cannot be sure if this works correctly or if the native >> built-in method is not overwritten by someone else. >> >> Is there a way to check if (for example: String.prototype.trim) is a >> real built-in method? > > The code inside native functions are syntatically incorrect. You can use > this to check them. For example, the "escape" function (any function will > do): > > var a=escape.toString(); > console.log(a); > //Firefox/Safari: "function escape() {\n [native code]\n}" > //Chrome/MSIE/Opera: "function escape() { [native code] }" > > You see that the code contains only an expression, which is an array. The > array contains only one expression, which is "native code". i.e.: two > references to identifiers which are neither reserved word, variable, > function nor object. This expression is not correct and will cause a syntax > error exception. I completely forgot about "[native code]". > The easiest way to check it is to use the "eval" function to check whether > an existing function is syntatically correct or not. > > function isNativeFunc(f) { > var a, z; > if (typeof f == 'function') { > //is a function > try { > a = f.toString(); > eval('(function(){'+a+'})()'); > return false; > } catch(z) { > //syntax error > return true; > } > } else { > //not a function. assume false > return false; > } > } > > console.log(isNativeFunc(escape)); > //shows: true > console.log(isNativeFunc(isNativeFunc)); > //shows: false > > Note that the usage of "eval" is considered as unsafe. So, since there's no > direct alternative for that method, parsing is neccessary. A regular > expression is used for this method. > > function isNativeFunc(f) { > if (typeof f == 'function') { > //is a function > var a = f.toString(); > //reg-exp for native function code block format > var r = /[^{]+\{[\n ]+\[[a-z]+ [a-z]+\][\n ]+\}$/im; > return a.match(r) != null; > } else { > //not a function. assume false > return false; > } > } > > console.log(isNativeFunc(escape)); > //shows: true > console.log(isNativeFunc(isNativeFunc)); > //shows: false Thank you for explanation. Make sense. I have no more questions in this topic. -- Cezary Tomczyk http://www.ctomczyk.pl/