Closed Thread Icon

Preserved Topic: dynamic object creation? Pages that link to <a href="https://ozoneasylum.com/backlink?for=17885" title="Pages that link to Preserved Topic: dynamic object creation?" rel="nofollow" >Preserved Topic: dynamic object creation?\

 
Author Thread
bunchapixels
Neurotic (0) Inmate
Newly admitted
posted posted 12-18-2000 06:16

hello peeples,
ive got a nifty littel javascript, and i want to create several instances of this object, where i would have:
block1 = new block('blockid')
right... but the thing is i want it to be done as the page generates:
like
i=1

eval('block' + i) = new block('block' + i)
i++
eval('block' + i) = new block('block' + i)
you get me?
what i want to know, is - is the eval() part right?? or is it something else?
if someone can enlighten me, that would be great.
<img border=0 align=absmiddle src="http://www.ozones.com/forum/smile.gif">

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 12-18-2000 18:47

Seems right to me. You may want to wrap those in a "for" loop instead of writing them all out by hand and incrementing the value but that should work fine.

Usually however you so want to create the objects on page load if you are going to manipulate existing elements on the page, instead of straight out of the Head


Walking the Earth like Kane

bunchapixels
Neurotic (0) Inmate
Newly admitted
posted posted 12-18-2000 22:30

yeah, i WOULD use a for loop usually.
BUT...
im using a mix of programs.
and the program i am using (aptrix, if you've heard of it) stores pages which can be categorised and stuff, and then can dynamically generate a 'menu' that is customisable (eg a menu of sale items, or latest 10 news articles....) and i want a little dynamic layer next to each one....
let the fun begin!
wish me luck!

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 12-19-2000 03:04

Man... I wish I had the slightest idea of what you guys were talking about. Well, maybe someday I'll get it. ????

bunchapixels
Neurotic (0) Inmate
Newly admitted
posted posted 12-19-2000 04:00

well butcher, did you know that in javascript, you can create your own objects?
then, your objects can have properties, like the predefined ones do.
(eg window.height - height is a property of the object window)
so, lets say we want to create a chair:
thisChair = new Chair()

then, all you need is a function chair:
function Chair() {
this.price=20
this.weight=2
this.color='blue'}

so now, the value thisChair.color is 'blue'.
SO, that means you can create a few chairs:

thisChair = new Chair()
thatChair = new Chair()

right? but what if you dont want them to be all $20, 2kg and blue?
then pass variables!
check this:
function Chair(price, weight, color) {
this.price=price
this.weight=weight
this.color=color
}

SO.... then, when you create each chair, you give it its own variables:

thisChair = new Chair(20, 1, 'blue')
thatChair = new Chair(10, 5, 'pink')

so now, thisChair.price = 20, but thatChair.price = 10.
making sense?

ok, well, what i want to do is create, say 20 blocks objects.
but instead of writing
block1 = new Block(block1ID) //creates an object called 'block1', whose attributes are the block1ID
block2 = new Block(block2ID) //creates an object called 'block2', whose attributes are the block2ID
block3= new Block(block3ID) //creates an object......
and so on, i want it to do some writing for me.
so, as bitdamaged suggested, i can have something like this:
for(i=1, i>20, i++) {
eval('block' + i + ' = new Block(block' + i + 'id)')
}

so what this does create 20 block objects, block1, block2, etc..
the purpose of the eval function is to use the i variable to create a string, then evaluate that function as a string. understand?
if we just had blocki = new block(blockiID), then it would create the one "blocki" object. but the eval function basically means that when i=1, for instance, it writes this code:
block1 = new Block(block1id)
then evaluates it (runs the code), then it will do the same sort of thing for i=2, and so on.


....and that's what we were talking about.
and as for my later post, "yeah, i WOULD use a for loop usually.
BUT..." and so on,
well, that's an entirely different, non javascript, matter. im just using a program that makes my html.
i hope this helps.

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 12-20-2000 22:10

Thank you Bunchapixels, That was a truly awesome explanation. Your explanation of creating an object was better than in any of the books I have paid for. My interest is piqued now though. If you ever get the chance, could you tell me what your doing with the 20 blocks? I find it much easier to understand things when I know why I would use them.

BTW: Sorry for the late reply, I didn't really expect anyone to answer my dumb interjection in your thread, but I'm greatfull you did.

bunchapixels
Neurotic (0) Inmate
Newly admitted
posted posted 12-20-2000 22:41

ah, butcher, i thought you would never ask!
well, i learnt OOP (obj oriented prog) in uni, and thought "fantastic. now, where is this useful?"
ok, well, for work, i wanted to create this special bullets (you'll see in my example), and this obviously needed some OOPing, because they were all basically the same object, just different instances, with slightly different properties.
well, here's what i made: http://reitsma.tripod.com/divslider.htm
um, what else can i say?
i wont drown you with a barrage of links, just give you one: http://www.dkrz.de/~k202039/dhtml/index.html
this one has examples and explanation, and the code is nice and clean.

[This message has been edited by bunchapixels (edited 20-12-2000).]

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 12-21-2000 00:24

I got them too http://www.bitdamaged.com let the smiley face catch up to your mouse and you'll see a bunch of individual objects




Walking the Earth like Kane

Bugimus
Maniac (V) Mad Scientist

From: New California
Insane since: Mar 2000

posted posted 12-21-2000 00:35

Me too, go here and see several different "instances" of one chaser "object":
http://bugimus.com/gurus/chaser_formation.html

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 12-21-2000 03:04

Very cool guys! Bunchapixels, your divslider.htm wouldn't come up. I got the Tripod couldn't find the page, page(does that make sense, sense). The other link you gave me looks terrific, thankyou for pointing out more stuff I don't know that I need to learn, haha. I appreciate it.

bunchapixels
Neurotic (0) Inmate
Newly admitted
posted posted 12-21-2000 05:31

http://reitsma.tripod.com/divslider.html
ok, i cut and paste it into the box, so if this doesnt work, i have no idea what is up.
far out! this is ridiculous!
i renamed it, maybe that will help: http://reitsma.tripod.com/stupidpage.html
ok, that seems to work.

[This message has been edited by bunchapixels (edited 21-12-2000).]

[This message has been edited by bunchapixels (edited 21-12-2000).]

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 12-21-2000 23:38

Thanks Bunchapixels, That's a great effect. I'm also very flattered to see you renamed the page with me in mind. (stupidpage.html) I'll spend alot of time looking over your code trying to get it, Thanks for your help.

bunchapixels
Neurotic (0) Inmate
Newly admitted
posted posted 12-21-2000 23:46

butcher - funny.
nah, it was renamed with tripod and the upload problem.
(ie, what should i rename this stupid bloody page to now?)

feel free to email me (or better yet put up another post to get some feedback from more educated persons), if you want me to explain the madness behind my methods.

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 12-22-2000 01:33

Thanks, I may take you up on that.

« BackwardsOnwards »

Show Forum Drop Down Menu