PDA

View Full Version : Creating Javascript Objects??


madman
12-26-2002, 07:38 PM
hi people

i am a student learning javascript and as part of my assignment we have to create some javascript objects. we have to create a webpage using javascript, most of the webpage should be created automatically by javascript and to that we been told to create objects for employees, lectuers and modules. i have created a object for employee, i wanted to know is this the right way of going for this assignment, i would be greatfull if anyone could sugest any ideas or any improvement for this code.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--

function Employee (id, name, dept, website, fileMc, pcMc) {
this.id = 00111
this.name = john
this.dept = Computing
this.username = "ku" + this.id
this.website = website || ("http://www.someurl.ac.uk/~" + this.username)
this.fileMc = fileMc || ("file://something.someurl.ac.uk/~" + this.username)
}

//-->
</script>


</head>
<body>

</body>
</html>

Thanks

ez4me2c3d
12-26-2002, 08:46 PM
sort of... take my "Person" example in to consideration.

<html>
<head>
<script>
function Person(first_name, middle_name, last_name, suffix, age, gender) {
this.name = new Name(first_name, middle_name, last_name, suffix);
this.fullname = first_name + " " + middle_name + " " + last_name
if (suffix) {
this.fullname += " " + suffix;
}
this.age = age;
this.gender = gender;
}

function Name(first, middle, last, suffix) {
this.first = first;
this.middle = middle;
this.last = last;
this.suffix = suffix;
}
</script>
</head>

<body>

<script>
anthony = new Person("Anthony", "Vernon", "Holloway", "Jr.", "20", "m");

document.write(anthony.name.first);

</script>

</body>
</html>

if you wanted to add a class to your object later you just type
anthony.isFat = "No";

get it?

madman
12-26-2002, 09:03 PM
thanks alot anthony for your help

whammy
12-27-2002, 12:14 AM
Hmm, I've never seen such a clear demonstration of this in javascript, either. This helps me as well, since I'm learning OOP (.NET - C#/VB) and would love to apply these concepts to javascript. :)

Adam20002
12-27-2002, 01:23 AM
For more javascript OOP fun a good two part tutorial i used is

part 1 : http://www.webmasterbase.com/article/470


part 2 : http://www.webmasterbase.com/article/473

whammy
12-27-2002, 01:49 AM
That's a cool tutorial, but isn't the first example kind of backwards? I guess that's cool if you do a double-take, though... ;)

madman
12-27-2002, 11:54 AM
thanks to everyone for helping and thank u adam2002 for the links, they are very usefull.

ca_redwards
12-27-2002, 09:32 PM
I wrote my HTML() bookmarklet library (http://www.angelfire.com/ca/redwards/html__.calendar.html) with exactly this goal in mind. :thumbsup: Once the library is invoked, it builds 154 new functions onto the window, String.prototype and Array.prototype objects to facilitate easy creation of complicated HTML constructs.

On the documentation page (linked above), there are examples that dynamically create a calendar, a websafe color palette, and even a web page editor. :D