Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
How to create an object using JavaScript?
How to create an object using JavaScript?
✍: Guest
Objects can be created in many ways. One way is to create the object and add the fields directly.
<script type="text/javascript">
var myMovie = new Object();
myMovie.title = "Aliens";
myMovie.director = "James Cameron";
document.write("movie: title is \""+myMovie.title+"\"");
<
This produces
movie: title is "Aliens"
To create an object you write a method with the name of your object and invoke the method with "new".
<script type="text/javascript">
function movie(title, director) {
this.title = title;
this.director = director;
}
var aliens = new movie("Aliens","Cameron");
document.write("aliens:"+aliens.toString());
</script>
This produces
aliens:[object Object]
You can also use an abbreviated format for creating fields using a ":" to separate the name of the field from its value. This is equivalent to the above code using "this.".
<script type="text/javascript">
function movie(title, director) {
title : title;
director : director;
}
var aliens = new movie("Aliens","Cameron");
document.write("aliens:"+aliens.toString());
</script>
This produces
aliens:[object Object]
2011-08-02, 4052👍, 0💬
Popular Posts:
Where Do You Download JUnit? Where do I download JUnit? I don't think anyone will ask this question ...
How Many Tags Are Defined in HTML 4.01? There are 77 tags defined in HTML 4.01: a abbr acronym addre...
What's the output of the following program? And why? #include main() { typedef union { int a; char b...
How To Recover a Dropped Index? - Oracle DBA FAQ - Managing Oracle Table Indexes If you have the rec...
Can JavaScript steal text from your clipboard? It is true, text you last copied for pasting (copy &a...