SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jul 2005
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using the this. Keyword

    Hi,

    I have this function that i have been following along with and i have come across some that i need clarification on..


    I understand how to create custom objects and assign them propertys and methods and i also understand how to add custom propertys and methods to preBuilt objects..

    -----------------------------

    Can someone please explain the flow of this function below.. I am getting a little lost when i get to this part

    this.param = function(name)
    {
    if (arguments.length) {
    if (defined(parameters[name]))
    return parameters[name][0];
    else
    return undefined;
    }

    ------ ALL HELP is very much appricated!

    function ParseQueryString()
    {
    var parameters = {};
    var parameterNames = [];

    /* defined for convience and readability */
    function defined(expr)
    {
    return expr != undefined;
    }

    function init()
    {
    var i;
    var pair, pairs;
    var name, value;

    if (location.search.length < 2)
    return;

    pairs = location.search.substr(1).split(/[&;]/);

    for (i = 0; i < pairs.length; i++)
    {
    pair = pairs[i].split(/=/);

    name = pair[0].decodeURL();
    if (defined(pair[1]))
    value = pair[1].decodeURL();
    else
    value = undefined;

    if (!defined(parameters[name])) {
    parameterNames.push(name);
    if (defined(value))
    parameters[name] = [value];
    else
    parameters[name] = [];
    }
    else if (defined(value))
    parameters[name].push(value);
    }
    }
    init();

    var scalarParamIndex = 0;

    this.param = function(name)
    {
    if (arguments.length) {
    if (defined(parameters[name]))
    return parameters[name][0];
    else
    return undefined;
    }
    else {
    if (scalarParamIndex < parameterNames.length)
    return parameterNames[scalarParamIndex++];
    else {
    scalarParamIndex = 0;
    return undefined;
    }
    }
    }

    this.params = function(name)
    {
    if (arguments.length) {
    if (defined(parameters[name]))
    return parameters[name].clone();
    else
    return null;
    }
    else
    return parameterNames.clone();
    }
    }

    Erik

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you post code to any forum on the internet, you use code tags to preserve the formatting of the code--so that it is easy to read. For information on how to use code tags, look at the bottom left hand corner of this page and click on 'vB code is On'.

    I understand how to create custom objects and assign them propertys and methods
    Do you know how to create a constructor function?
    Code:
    function Dog(theName, theBreed, barkFunc)  //constructor function
    {
    	this.name = theName;
    	this.breed = theBreed;
    	this.bark = barkFunc;
    }
    
    function barkFunc()
    {
    	alert("Grrrrrr...woof!");
    }
    
    var myDog = new Dog("Max", "labrador", barkFunc);
    alert(myDog.name);
    myDog.bark();
    Constructor functions are characterized by lines starting with 'this':
    Code:
    this.name = theName;
    this.breed = theBreed;
    this.bark = function(){alert("woof")};
    this.move = NameOfAFuncThatIsDefinedElsewhere;
    and to call constructor functions you use the new keyword:
    Code:
    var myDog = new Dog(...);
    Constructor functions create objects with the properties and functions listed in the function definition.
    Last edited by 7stud; Jan 18, 2007 at 21:58.


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
  •