/    Sign up×
Community /Pin to ProfileBookmark

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?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@GullanianMay 06.2010 — You have to [I]create[/I] the object first [I]after[/I] defining it.

var myObject = new myObj();

Then you can reference myObject anywhere in your scripts once it has been created.
Copy linkTweet thisAlerts:
@mrhooMay 06.2010 — <script type="text/javascript">

var myObj = {name : "foo",age: 5};

alert(myObj.name)// also window.myObj

</script>
Copy linkTweet thisAlerts:
@NicTltMay 06.2010 — You have to [I]create[/I] the object first [I]after[/I] defining it.

var myObject = new myObj();

Then you can reference myObject anywhere in your scripts once it has been created.[/QUOTE]


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).
Copy linkTweet thisAlerts:
@Declan1991May 06.2010 — 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.<i>
</i>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.
Copy linkTweet thisAlerts:
@NicTltMay 06.2010 — Jinx!
Copy linkTweet thisAlerts:
@cyberhiteshJul 31.2010 — <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/javascript/json-in-javascript
×

Success!

Help @wootton spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...