Static variable in Javascript?

Discussion in 'Javascript' started by seans, Aug 17, 2005.

  1. seans

    seans Guest

    Hi,

    Is there a way to create a static variable in Javascript? I want to use
    a recursive function to traverse a DOM tree and examine each node. If
    the node is a text object I would like to save the text to a string or
    array. Is it possible to create an array or string variable to do this?

    Any advice would be greatly appreciated. Thanks again.

    sean
     
    seans, Aug 17, 2005
    #1
    1. Advertising

  2. seans

    VK Guest

    seans wrote:
    > Hi,
    >
    > Is there a way to create a static variable in Javascript? I want to use
    > a recursive function to traverse a DOM tree and examine each node. If
    > the node is a text object I would like to save the text to a string or
    > array. Is it possible to create an array or string variable to do this?
    >
    > Any advice would be greatly appreciated. Thanks again.
    >
    > sean


    Static means shared by all object instances (other words it's a
    constant in the constructor referred by all created objects). ECMA
    variant of JavaScript doesn't have (yet?) neither constant nor static
    types but it can be more-or-less successfully emulated. Is it what
    you're looking for? Because from your task description the necessity of
    this type is not evident. You rather need a global array (?)
     
    VK, Aug 17, 2005
    #2
    1. Advertising

  3. On 17/08/2005 11:37, VK wrote:

    [snip]

    > Static means shared by all object instances


    Unless the OP is referring to function-oriented statics. The only
    reference the OP makes to objects is with regard to text nodes.

    > (other words it's a constant in the constructor referred by all
    > created objects).


    A static in no way implies a constant value. Static members can be
    constant, but they don't have to be.

    > ECMA variant of JavaScript doesn't have (yet?) neither constant nor
    > static types but it can be more-or-less successfully emulated.


    To clarify, constants are not available in ECMAScript. However, both
    private and public static members can be implemented. The former uses a
    closure outside the constructor function. The latter is provided simply
    by adding properties to the constructor function.

    var MyObject = (function() {
    /* Declare private static variables here */

    function constructor() {
    /* ... */
    }
    constructor.myStatic = 'myValue';

    return constructor;
    })();

    MyObject.myStatic // 'myValue'

    [snip]

    Mike

    --
    Michael Winter
    Prefix subject with [News] before replying by e-mail.
     
    Michael Winter, Aug 17, 2005
    #3
  4. seans wrote:

    > Is there a way to create a static variable in Javascript? I want to use
    > a recursive function to traverse a DOM tree and examine each node. If
    > the node is a text object I would like to save the text to a string or
    > array. Is it possible to create an array or string variable to do this?


    The term "static variable" has probably different meanings depending on
    whom you ask or which existing programming language you have in mind.
    As for that recursive function, can't you simply return the text as
    needed or pass the text string (or array if needed) around with each
    function call?
    Imagine you wanted to concatenate the text node value of all descendants
    of a node then you could do that alike (pseudo code not meant to be a
    complete implementation covering all node types)
    function getInnerText (node) {
    if (node.nodeType == 3) {
    return node.nodeValue;
    }
    else if (node.nodeType == 1) {
    var innerText = '';
    for (var i = 0; i < node.childNodes.length; i++) {
    innerText += getInnerText(node.childNodes);
    }
    return innerText;
    }
    else {
    return '';
    }
    }
    so that approach would simply return the string from each recursive call
    and concatenate it as needed.
    Or you could build an array if strings if you prefer that and pass it
    along e.g. (again pseudo code not meant to be a complete implementation)
    function getInnerTextArray (node, textArray) {
    if (typeof textArray == 'undefined') {
    textArray = [];
    }
    if (node.nodeType == 3) {
    textArray[textArray.length] = node.nodeValue;
    }
    else if (node.nodeType == 1) {
    for (var i = 0; i < node.childNodes.length; i++) {
    getInnerTextArray(node.childNodes, textArray);
    }
    }
    return textArray;
    }


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Aug 17, 2005
    #4
  5. seans

    seans Guest

    Thanks everyone for all your help.

    sean
     
    seans, Aug 17, 2005
    #5
  6. seans

    seans Guest

    Thanks everyone for all your help.

    sean
     
    seans, Aug 17, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kookey
    Replies:
    3
    Views:
    20,801
    Andrew Thompson
    Aug 20, 2005
  2. Patrick Hoffmann
    Replies:
    3
    Views:
    2,878
    Christian Jan├čen
    Aug 8, 2003
  3. Marcin Vorbrodt

    Global static variable vs static method

    Marcin Vorbrodt, Sep 5, 2003, in forum: C++
    Replies:
    3
    Views:
    5,440
    Denis Perelyubskiy
    Sep 5, 2003
  4. Sam
    Replies:
    4
    Views:
    487
    The Directive
    Jan 13, 2004
  5. Vikram Kalra
    Replies:
    23
    Views:
    898
    Nigel Wade
    Sep 7, 2007
Loading...

Share This Page