SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2008
    Location
    Hot Springs, Montana
    Posts
    195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    _Simply Javascript_ question

    In regard to the subsection, "starting your first function," in _Simply Javascript_ (pages 74-79), a sitepoint book, could I beg for some clarification?

    Particularly, concerning this code:
    Code:
    var pattern = new RegExp ("(^| )" + theClass + "( |$)");
    for (var i=0; i < elementArray.length; i++)
    {
        if (pattern.test(elementArray[i].className))
       {
          .
          .
          .
       }
    }
    The book refers to theClass as a variable against which the property className, a string, is compared. But, how is a property compared with a variable, such as theClass. I would think that a property could only be compared to another property (say, the value of a variable). How does a property like className get compared to a variable like theClass? (className does not equal or look like theClass according to my eyes.) Or, how does the value or property of this variable (theClass) get defined with some string against which the string elementName can be compared? Does some hidden command or protocol automatically define the value of theClass to be the name of that element/class, so that the element name might or might not equal "className"? Where is that hidden command? How does it work? Perplexed.

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The className property is a string. Everything in an object will be of a certain type, just like all objects will be of a certain type (strings are String objects, integers are Number objects). For example, here is an object called myObj with three properties, which are of different types (string, array, integer):
    Code:
    var myObj = {
      str: 'This is a string',
      arr: ['this','that'],
      int: 23098
    }
    Then myObj.str is a string. myObj is an object of the Object type (a bit confusing).

    theClass (a variable) is a string. So it and myObj.str and elementArray[i].className are all strings and thus you can run that test comparing the className of an element against the regular expression, formed by creating a new RegExp object (another of JavaScript's objects). In creating this object, you pass it a single parameter, the string which is a concatenation of some regexy stuff and theClass (a simple string). theClass will have been defined elsewhere, possibly as simply as something like this:
    Code:
    var theClass = 'thing';
    In case it helps, this:
    Code:
    if (pattern.test(elementArray[i].className)) { ...
    could be rewritten as:
    Code:
    var elClass = elementArray[i].className; // elClass is a string
    if (pattern.test(elClass)) {...

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2008
    Location
    Hot Springs, Montana
    Posts
    195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    got it

    Thanks. I don't know why, but it clicked into place. Somehow, I had turned around my understanding of the property/variable relationship. (Ack.) This script does make sense to me after all. I appreciate your help!


Bookmarks

Posting Permissions

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