Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #16765

Re: Built-in native method

Path csiph.com!usenet.pasdenom.info!aioe.org!.POSTED!not-for-mail
From Cezary Tomczyk <cezary.tomczyk@gmail.com>
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 <k618t0$2d5$1@speranza.aioe.org> (permalink)
References <k5sbuj$bt8$1@speranza.aioe.org> <XnsA0F38C4E46019jaejunksgooglemailco@0.0.0.44>
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

Show key headers only | View raw


W dniu 2012-10-21 08:44, JJ pisze:
> Cezary Tomczyk <cezary.tomczyk@gmail.com> 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/

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Built-in native method Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2012-10-19 22:07 +0200
  Re: Built-in native method JJ <jaejunks_at@_googlemail_dot._com> - 2012-10-21 06:44 +0000
    Re: Built-in native method Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2012-10-21 18:46 +0200
    Re: Built-in native method Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2012-10-21 19:37 +0200
      Re: Built-in native method Andreas Bergmaier <andber93@web.de> - 2012-10-22 15:47 +0200
        Re: Built-in native method Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2012-10-22 18:00 +0200
          Re: Built-in native method Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-10-24 02:17 +0100
            Re: Built-in native method Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2012-10-24 22:17 +0200
              Re: Built-in native method Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-10-25 00:32 +0200
  Re: Built-in native method Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-10-24 02:15 +0100
    Re: Built-in native method Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2012-10-24 22:35 +0200
      Re: Built-in native method Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-10-25 00:03 +0200
        Re: Built-in native method Asen Bozhilov <asen.bozhilov@gmail.com> - 2012-10-26 03:58 -0700
          Re: Built-in native method RobG <rgqld@iinet.net.au> - 2012-10-29 16:39 -0700
        Re: Built-in native method Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2012-10-28 15:32 +0100
          Re: Built-in native method Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-10-28 17:05 +0100
            Re: Built-in native method Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2012-10-28 18:39 +0100
              Re: Built-in native method Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-10-28 20:10 +0100
                Re: Built-in native method Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2012-10-28 20:23 +0100
                Re: Built-in native method Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-10-28 21:23 +0100
                Re: Built-in native method Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2012-10-28 22:03 +0100
        Re: Built-in native method Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2012-10-28 15:49 +0100
          Re: Built-in native method Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-10-28 17:12 +0100
            Re: Built-in native method Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2012-10-28 17:49 +0100
              Re: Built-in native method Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-10-28 18:07 +0100
                Re: Built-in native method Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2012-10-28 19:22 +0100
                Re: Built-in native method Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-10-28 19:53 +0100

csiph-web