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


Groups > comp.lang.javascript > #16762

Re: Built-in native method

From JJ <jaejunks_at@_googlemail_dot._com>
Newsgroups comp.lang.javascript
Subject Re: Built-in native method
Date 2012-10-21 06:44 +0000
Organization A noiseless patient Spider
Message-ID <XnsA0F38C4E46019jaejunksgooglemailco@0.0.0.44> (permalink)
References <k5sbuj$bt8$1@speranza.aioe.org>

Show all headers | View raw


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.

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

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