www.webdeveloper.com
Results 1 to 6 of 6

Thread: Counting the number of methods in javascript

  1. #1
    Join Date
    Dec 2008
    Posts
    1

    Counting the number of methods in javascript

    Hi folks,

    I am doing a small project where I have to count the number of basic functions and methods as concat, link, replace, toString,.. in any JavaScript code. My idea is to use string comparisons with each keyword in javascript code. However this solution maybe not right or not enough.

    Can anyone help me ?
    many thanks,

  2. #2
    Join Date
    Nov 2008
    Posts
    157
    What do you mean by basic functions?
    xp

  3. #3
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by javascript_bebe View Post
    Hi folks,

    I am doing a small project where I have to count the number of basic functions and methods as concat, link, replace, toString,.. in any JavaScript code. My idea is to use string comparisons with each keyword in javascript code. However this solution maybe not right or not enough.

    Can anyone help me ?
    many thanks,
    Functions are always custom in javascript, so that there are no "basic functions". You probably mean native methods.

    Well, this is a huge and intricate project. Methods should be classified upon the objects/elements whom they are act upon. But here rises a problem. There is a standard (ECMAScript and W3C DOM), but there is no browser to follow entirely the standards. Each browser has some methods of his own. IE has even his own javascript language, called JScript, which is more or less javascript 1.2 to 1.4 compatible. There are a lot of exceptions and a lot of variants/bugs amongst browsers.

    Moreover, which javascript version you intend to analyze:1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0 ? On which browsers' generation: 4,5,6,7,8?

  4. #4
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,755
    You could try using a recursive for-in loop to iterate through EVERY object, starting at the window object. If the type of the object's property is "function", call that property's toString method. If it contains "[native code]" then it is a native function.

    Of course this would bring any browser and computer to its knees.

  5. #5
    Join Date
    Feb 2006
    Posts
    2,926
    If you are trying to count the native object methods in a string or file of javascript, you have to first separate quoted strings and regular expressions from the search, then go through the rest of the string with a regular expression that matches the pattern of a method call.

    For each match, loop through an array of native objects
    [Function,Object,String,Array,Date,Error,Math,Number,RegExp]
    and increment your count (and quit the loop) if any of their prototypes include as a property the matched string- and it is not enumerable.

    It gets complicated if you use any native method identifiers as method names for your own objects, so don't .
    Last edited by mrhoo; 12-11-2008 at 09:11 AM.

  6. #6
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    i think a string-based approach matching the program code would be the simplest way.

    you can find all the natives listed at w3 schools.
    since it is case sensitive, writing something that matches a list of names is fairly simple.

    what's more fun (and complicated) is building a dependency tree; scan window for names, scan functions to find names, scan newly found functions' code for more/new functions. repeat until the result is the same twice in a row and you have a standalone package..

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles