DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2010
    Posts
    17

    Help with javascript custom objects and parameters

    I have an object that is defined by and object function. The object takes some parameters and I need one of them to be a string thats the same as the variable name for the object. e.g. var myObj = new form("myObj", arg2, arg3);

    What I want to know is if there is any way to find out if the first argument is the same as the variable name of the object.

  2. #2
    Join Date
    Feb 2010
    Posts
    17
    Wow, I just figured out the answer on my own. I was working on a different part of the same project and I was using the setTimeout(). I was trying to pass the string of the id property of my object. I kept getting an error and I found out that I was passing the object itself. So I figured out that if you have a string variable of a object name and pass that as a argument to a function using the setTimeout(), the argument will be that object.

    Here is a basic sample of the code I came up with:

    Code:
    //global variable
    var global_is_a_object;
    
    //the object function
    function myObject(id, arg2, arg3)
    {
            //properties
            this.id = id;
            this.something = arg2;
            this.something_else = arg3;
            
            //this is a check variable
            this.is_obj = "yes";
            //set global variable to false
            global_is_a_object = false;
            
            //create a variable for the id if this does not happen
            //and the id does not match the variable name a undefined
            //variable will be passed to the check function
            window[id] = "";
            
            //call function to start the check function
            some_function(id);
    }
    //this function starts the check
    function some_function(theID)
    {
            //when the setTimeout function is set up like this, the variable: theID
            //is passed to the function as a variable named the value of the theID variable
            var timmerID = setTimeout("isAObject(" + theID + ")", 1);
            //so if the theID has a value of "myObj" then what we're doing here is
            //calling the function like this: isAObject(myObj)
    }
    //the check function
    function isAObject(obj)
    {
            //if obj has a type of an object
            if(typeof(obj) == "object") {
                    //check the property
                    if(obj.is_obj == "yes") { global_is_a_object = true; }
                    else { global_is_a_object = false; }
            } else { global_is_a_object = false; }
            
            if(global_is_a_object) {
                    //we're good
                    alert("the id is the same");
            } else {
                    //we're not good
                    alert("the id is not the same");
            }
    }
    //set objects
    var myNewObject = new myObject("myNewObject", "blah", "something");
    var myNewObject2 = new myObject("myNewObject2", "blah", "something");

Similar Threads

  1. Replies: 5
    Last Post: 06-27-2008, 06:27 PM
  2. Passing Objects as Parameters.
    By md in forum Database
    Replies: 2
    Last Post: 05-31-2001, 08:59 AM
  3. passing uniqueidentifier parameters to command objects
    By Michael Shutt in forum VB Classic
    Replies: 2
    Last Post: 12-29-2000, 07:55 AM
  4. Parameters collection
    By martin rydman in forum VB Classic
    Replies: 8
    Last Post: 12-21-2000, 04:18 AM
  5. Components vs Objects
    By Oliver Lennon in forum Enterprise
    Replies: 3
    Last Post: 04-18-2000, 12:30 PM

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
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links