# Object literals

**URL:** <https://forum.kirupa.com/t/object-literals/16123>\
**Category:** Uncategorized\
**Created:** [March 21, 2003, 3:51am UTC](https://forum.kirupa.com/t/object-literals/16123 "2003-03-21T03:51:31Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![chapster](https://avatars.discourse-cdn.com/v4/letter/c/958977/32.png) [@chapster](https://forum.kirupa.com/u/chapster)\
**Post date:** [March 21, 2003, 3:51am UTC](https://forum.kirupa.com/t/object-literals/16123/1 "2003-03-21T03:51:31Z")

</div>

I can get this to work on attachMovie but it won’t work in this case.

```auto

_root.createEmptyMovieClip("temper", 1,{_x:60, _y:40});
trace(temper._x);

```

What am I missing?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 21, 2003, 6:05am UTC](https://forum.kirupa.com/t/object-literals/16123/2 "2003-03-21T06:05:21Z")

</div>

You are not missing anything… You are adding more argument in there though…

```auto
myMovieClip.createEmptyMovieClip (instanceName, depth)

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 21, 2003, 2:21pm UTC](https://forum.kirupa.com/t/object-literals/16123/3 "2003-03-21T14:21:07Z")

</div>

Yes I am aware of that but I was using the object literal as I have seen it been used and was interested in seeing how it worked. As I said I used the same set up in attachMovie and it worked.

```auto

_root.attachMovie("temper","temp",2,{_x:200,_y:70});
trace(temp._x);

```

The trace shows 200  
This works now I have seen the same object literal use in the

```auto

_root.createEmptyMovieClip("test",3,{_x:40,_y:200});
trace(test._x);

```

trace here shows 0  
IS this more clear  
And why can"t it be done on the creatEmptyMovieClip

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 21, 2003, 2:29pm UTC](https://forum.kirupa.com/t/object-literals/16123/4 "2003-03-21T14:29:51Z")

</div>

> I have seen it been used  
> Are you saying that somebody used that initialization object with the createEmptyMovieClip() function or attachMovie() function???

Yes, you can use that with attachMovie() function since Macromedia gave us a room to use with, but not with the createEmptyMovieClip() function… createEmptyMovieClip() function just takes two arguments… No more than that unless there is some sort of undocumented function somewhere… (You could create the prototype that takes object literal though, but I don’t think that will be so useful though…) Check out the ActionScript dictionary and compare two functions side by side… 😉

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 21, 2003, 2:32pm UTC](https://forum.kirupa.com/t/object-literals/16123/5 "2003-03-21T14:32:56Z")

</div>

I am checking into right now.  
your probably right  
it can’t be used but I saw someone use it but I didn’t check to see if it worked for them.  
Thanks for your replies

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 21, 2003, 2:40pm UTC](https://forum.kirupa.com/t/object-literals/16123/6 "2003-03-21T14:40:18Z")

</div>

You’re welcome… Please let me know after you checking it out… 😉

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 22, 2003, 6:11am UTC](https://forum.kirupa.com/t/object-literals/16123/7 "2003-03-22T06:11:33Z")

</div>

No your right it does not work for createEmptyMovieClip. I checked everything  
Thanks for your help on this.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 22, 2003, 8:07am UTC](https://forum.kirupa.com/t/object-literals/16123/8 "2003-03-22T08:07:08Z")

</div>

No problem… Drop by anytime… 😉

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 22, 2003, 2:19pm UTC](https://forum.kirupa.com/t/object-literals/16123/9 "2003-03-22T14:19:52Z")

</div>

yeah I thought it was pretty recokulous not to include that option for createEmptyMovieClip. You can make your own version to do that though.

```auto

MovieClip.prototype.$createEmptyMovieClip = MovieClip.prototype.createEmptyMovieClip;
MovieClip.prototype.createEmptyMovieClip = function(name, depth, init){
	var mc = this.$createEmptyMovieClip(name, depth);
	init. __proto__ = null;
	for (var prop in init) mc[prop] = init[prop];
	return mc;
}

```

just throw that in the first frame of your movie to immediately initialize it, then you can use createEmptyMovieClip with an init object just like attachMovie

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 22, 2003, 2:37pm UTC](https://forum.kirupa.com/t/object-literals/16123/10 "2003-03-22T14:37:22Z")

</div>

Nice one, senocular… 😉

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 22, 2003, 3:08pm UTC](https://forum.kirupa.com/t/object-literals/16123/11 "2003-03-22T15:08:29Z")

</div>

cool can you explain it a little. I love when people give you these things but I always have to know how it was built and what each line does.  
If you can take the time to do that it would be super cool  
also I have never seen this been used  
$createEmptyMovieClip  
the $ sign that is before the function.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 22, 2003, 3:47pm UTC](https://forum.kirupa.com/t/object-literals/16123/12 "2003-03-22T15:47:29Z")

</div>

$ is just another character that can be used in variable names. What Im doing with

```auto
MovieClip.prototype.$createEmptyMovieClip = MovieClip.prototype.createEmptyMovieClip;

```

is just saving the original createEmptyMovieClip into a new variable called $createEmptyMovieClip. I didnt have to use $createEmptyMovieClip, I could have just as well said bob, but I need to use the original createEmptyMovieClip in my new function so I have to save it. I cant just use createEmptyMovieClip because the new function Im making IS createEmptyMovieClip so calling createEmptyMovieClip within itself would be calling itself and would cause an infinite recursion loop. The $ is seen with PHP and other languages of the such which makes it comfortable for those kinds of programmers to use Flash - but its common to use it in situations like this in renaming a pre-existing function or setting up something thats tied into a system event or something… but its nothing set in stone.

createEmptyMovieClip is kept within the movieclip prototype object which is an object that holds functions and variables for all movieclips. So any movieclip has access to using createEmptyMovieClip as long as its in the MovieClip.prototype. You can just as well make your own prototypes for MovieClips or other objects like Array and String etc. See [http://proto.layer51.com](http://proto.layer51.com) for more.

```auto
MovieClip.prototype.createEmptyMovieClip = function(name, depth, init){

```

defines our new re-write of createEmptyMovieClip for the MovieClip.prototype and allows 3 arguments; the original name and depth and the new init argument which is an initiation object as seen with attachMovie.

```auto
var mc = this.$createEmptyMovieClip(name, depth);

```

here a new movieclip, mc, is created using the saved $createEmptyMovieClip which is the original createEmptyMovieClip function.

```auto
init. __proto__ = null;

```

This takes away the prototype reference from the init object. Without this, since the init object IS an object, the following for in loop will not only cycle through the properties you specified in it, but also all the properties within the Object.prototype, which, like the MovieClip.prototype, holds functions and variables which are cosistent with all Objects. We dont want those to be set for the movieclip being created, just the ones manually specified in the actual init object. The **proto** property tells the object where to find its Object.prototype. Setting the **proto** of the init object to null means that it looks at null to find all of its common properties and functions. This way the inti object no longer references the Object.prototype to look for common properties of the such… instead it looks at null, and of course theres nothing there 😉

```auto
for (var prop in init) mc[prop] = init[prop];

```

The for in loop loops through a specified object going through all its properties/variables/functions. This includes the prototype functions (which arent explicitly hidden) which is why we needed the previous line. Here we are cycling through the init object and all the properties within it. For each loop, the current property is assigned to the prop variable. This is then used to set (or assign) that variable in the movieclip created, mc, to the value it has in the init object using associative array referencing. For more on that see  
[http://www.kirupaforum.com/forums/showthread.php?s=&threadid=12082](http://www.kirupaforum.com/forums/showthread.php?s=&threadid=12082)  
After that, the mc is returned with

```auto
return mc;

```

and the world is a happier place 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 22, 2003, 3:57pm UTC](https://forum.kirupa.com/t/object-literals/16123/13 "2003-03-22T15:57:39Z")

</div>

Cool thanks so much  
Most people wont spend the time doing this because half them just copy it from someone else and don’t even know the answers themselves.

I am going to study this now thanks again.  
🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 23, 2003, 7:12am UTC](https://forum.kirupa.com/t/object-literals/16123/14 "2003-03-23T07:12:22Z")

</div>

Great explanation, senocular… 😉

As one of those people who just copies other people’s good stuff, I really appreciate the explanation… :beam:

Your explanation made me remember one thing that I have read a while ago… It was about the usage of **proto** in FMX… This is the paragraph from the ActionScript Coding Standards…

> 

_ **Object Inheritance** _  
Setting the “ **proto** ” property to create inheritance was the practice with Macromedia Flash Player 5. This practice is unsupported in Macromedia Flash Player 6 and is not recommended. **proto** should be treated as a read-only property. The correct way to do accomplish inheritance is to create a prototype chain. To create a prototype chain, set the “prototype” property of the subclass constructor function to an instance of the superclass using the following syntax:  
_ChildClass.prototype = new ParentClass();_

\*\*An example of this practice: \*\*

```auto
function Shape() 
{ 
} 
function Rectangle() 
{ 
} 
Rectangle.prototype = new Shape(); 

```

\*\*The following practice is NOT recommended: \*\*  
\*Rectangle.prototype. **proto** = Shape.prototype; \*  
If developers are concerned that this form of inheritance will lead to all of the constructor code being called unnecessarily, the following code can prevent this:

```auto
_global.SuperClassConstructor = function() { 
  if (this._name!=undefined) { 
    // real constructor code goes here 
  } 
} 

```

In the above code, the constructor code will not execute because there is no instance defined yet. Note that this code only works with Movie Clip-based classes.  
Why am I quoting all this??? I just want to hear your opinion on this…  
Can this thing similar to random() and Math.random()??? Lots of people use random() no matter what, and we are not experiencing any problem yet… Or is it something more significant than random()???

Appreicate your great explanation again… :beam:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 23, 2003, 7:30am UTC](https://forum.kirupa.com/t/object-literals/16123/15 "2003-03-23T07:30:07Z")

</div>

Senocular: I hate you.

::runs away jealous::

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 23, 2003, 7:36am UTC](https://forum.kirupa.com/t/object-literals/16123/16 "2003-03-23T07:36:46Z")

</div>

How did you know that is the exact feeling that I have on him??? :beam:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 23, 2003, 7:38am UTC](https://forum.kirupa.com/t/object-literals/16123/17 "2003-03-23T07:38:00Z")

</div>

Because, hes too good… I think most people here have that feeling of him. At least he shares his knowledge, that rocks!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 23, 2003, 7:42am UTC](https://forum.kirupa.com/t/object-literals/16123/18 "2003-03-23T07:42:31Z")

</div>

Yup… That’s why I like him… :beam:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 23, 2003, 3:11pm UTC](https://forum.kirupa.com/t/object-literals/16123/19 "2003-03-23T15:11:33Z")

</div>

:!: I didnt know **proto** was “unsupported” in 6. I know its not _standard_ or really _recomended_ to use it, but I was hoping it’d be around for a while because its really nice to have… this example being a good … example of how it can easily solve that problem.

As for my take on it, if it works, Ill use it. I use random now because its less typing 😉 and its faster than Math.random(). **proto** I dont use often, and typically use the ‘correct’ MM method for inheritance, but for things like this, I find it to be the best, quickest solution for removing and prototype references. Actually… hmmm Ive never tried to do it without using **proto** … I wonder how it would be done then :thinks:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 23, 2003, 3:12pm UTC](https://forum.kirupa.com/t/object-literals/16123/20 "2003-03-23T15:12:06Z")

</div>

Dont hate me because Im beautiful :flower:

[Next page](https://forum.kirupa.com/t/object-literals/16123.md?page=2)
