www.webdeveloper.com
Results 1 to 7 of 7

Thread: Accessing JavaScript objects?

  1. #1
    Join Date
    May 2007
    Posts
    16

    Accessing JavaScript objects?

    Is there a way to access JavaScript objects in the same way HTML elements can be access from JavaScript. For example, if I had a document which contained the following JavaScript code:

    <script language="JavaScript">
    var myObj = {
    this.name = "foo";
    this.age = 5;
    }
    </script>

    How could I access myObj? Is it a property of the document object? For example, could I do:

    document.myObj.name = "new name";

    I realise this isn't actually necessary. However for the purpose of the question, I do need to be able to access the JavaScript objects from either the window or document objects?

    In other words, where to JavaScript objects in the JavaScript context reside? In the document object? In the window object?

  2. #2
    Join Date
    May 2010
    Posts
    44
    You have to create the object first after defining it.

    var myObject = new myObj();

    Then you can reference myObject anywhere in your scripts once it has been created.

  3. #3
    Join Date
    Feb 2006
    Posts
    2,927
    <script type="text/javascript">
    var myObj = {name : "foo",age: 5};

    alert(myObj.name)// also window.myObj
    </script>

  4. #4
    Join Date
    Apr 2010
    Location
    UK
    Posts
    117
    Quote Originally Posted by Gullanian View Post
    You have to create the object first after defining it.

    var myObject = new myObj();

    Then you can reference myObject anywhere in your scripts once it has been created.
    As mrhoo has succinctly demonstrated, that's not strictly true since javascript does not follow the classical inheritance pattern. There is no need to define a class and then instantiate it - a javascript object acts both as an instance and as a prototype for further objects.

    Any objects you define belong to the global namespace, which in the case of a browser is indeed the window object (as per mrhoo's last comment).

  5. #5
    Join Date
    Aug 2007
    Posts
    3,767
    You are mixing up ways to define objects. mrhoo's is the best, JSON, it's short and concise but clear. Another possibility is this, which is normally used as I'm using it, for multiple possibilities, people in this case.
    Code:
    var Person = function (name, age) {
    this.name = name;
    this.age = age;
    };
    var John = new Person("John",20);
    alert(John.name);
    And to elaborate on mrhoo's answer, all global variables are members of the window object, hence window.alert, window.document, window.anyGlobalVariableOrFunction. The window is optional though, which is why you don't always need it.

  6. #6
    Join Date
    Apr 2010
    Location
    UK
    Posts
    117
    Jinx!

  7. #7
    Join Date
    Jul 2010
    Posts
    1
    <script type="text/javascript">
    var employeeDetails = {
    "name" : "HiteshA",
    "country" : "India",
    "industry" : "Information Technology",
    "technology" : "Java/J2EE"
    }

    function iterateEmployeeDetails() {
    for(var data in employeeDetails) {
    alert("Key:" + data + " Values:" + employeeDetails[data]);
    }
    }
    window.onload = iterateEmployeeDetails;
    </script>
    The above code will show an alert message having key and value inside the json object.

    I have already written an article on this topic which includes nested JSON object as well, you can check it out at: http://www.hiteshagrawal.com/javascr...-in-javascript

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