Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 08-03-2001, 05:34 AM   #1
robertpenner
Registered User
 
Join Date: Apr 2001
Location: Vancouver
Posts: 36
Default

Wanted to share my own explanation of the prototype, __proto__, and constructor properties. This has suddenly become very clear to me recently, so I hope this helps someone.

To elaborate on __proto__ and prototype, they are similar, but not exactly the same. The difference is that __proto__ is a property of an object (usually a class instance), while prototype is a property of a constructor function. An object's __proto__ is a reference to its constructor's prototype.

Example:
Code:
// constructor for the Game class
function Game () {
// code
}

// create a method for the Game class
Game.prototype.printMe = function () {
trace "Game";
}

// create an object that's an instance of the Game class
chess = new Game();
chess.printMe() // output: "Game"
- now you have a class instance (chess) and a class constructor (Game)
- the name of the constructor function determines the name of the class
- the Game function is automatically given a prototype property
- we can use Game's prototype property to attach methods (like printMe)

So far so good? Onward...

- chess is created with the Game constructor, so it belongs to the Game class
- chess is automatically given a __proto__ property that points to the prototype property of Game
- this means that chess.__proto__ == Game.prototype

Some other info that's interesting:

1)
- Game.prototype has a __proto__ property as well
- Game.prototype.__proto__ == Object.prototype
- thus, chess.__proto__.__proto__ == Object.prototype

2)
- you can reference the Game constructor function with chess.__proto__.constructor
- suppose you wanted to make an object similar to chess, but you didn't know what class it belonged to
- you could make the object by accessing chess' constructor:
checkers = new chess.__proto__.constructor();

3)
- Game.prototype.constructor is the same as Game
- some people use something like "Game.prototype.constructor" in class inheritance
- it works, but it's redundant


[there's also a good explanation on p.269 of "Actionscript: The Definitive Guide" by Colin Moock]
robertpenner is offline   Reply With Quote
Old 08-03-2001, 09:36 AM   #2
Mortimer Jazz
Thing
 
Mortimer Jazz's Avatar
 
Join Date: Jun 2001
Location: UK
Posts: 2,418
Default

Thanks Robert. I recently c&p'd little of your previous explanation of prototypes in to one of the messages here (I credited you for it so I hope you don't mind).

Thanks for the info on __proto__. I look forward to the day when I can understand it (heheh!)
Kind regards,
Mort
Mortimer Jazz is offline   Reply With Quote
Old 08-03-2001, 12:07 PM   #3
red penguin
[^\d\D]
 
red penguin's Avatar
 
Join Date: Jun 2001
Location: Brooklyn, NY
Posts: 3,254
Thumbs up mercy buckets

in response to penner...thanks for the insight...I've been trying to nail these things down recently and you definitely have helped. You da man!

mort...moock does a good job with this in the book but it leaves you hanging. But with kind souls like penner here, we need not worry!
__________________

komielan.com
red penguin is offline   Reply With Quote
Old 02-06-2005, 04:29 AM   #4
Reflex
Love you long time
 
Join Date: May 2001
Location: Sydney, Australia
Posts: 299
Default

This may be one of the most helpful threads I have read in a long time. Thanks for the simple explanation. Brilliant
Reflex is offline   Reply With Quote
Old 11-28-2005, 02:36 PM   #5
Ruben
Me
 
Ruben's Avatar
 
Join Date: Jul 2002
Location: Netherlands, Europe
Posts: 3,512
Send a message via MSN to Ruben Send a message via Skype™ to Ruben
Default

Excellent man! You really helped me out here

- Ruben
__________________
.
Originally Posted by MichaelxxOA
"He sounded like he knew which words he was going to use before he knew what he was going to say. I hate that shit."
Ruben is offline   Reply With Quote
Old 12-11-2005, 05:04 AM   #6
Flash Gordon
rather be programming
 
Flash Gordon's Avatar
 
Join Date: Feb 2005
Location: City of Angels
Posts: 10,140
Default

I have been trying to read about this stuff recently, and I just don't get it. Is there a dumbed-down version somewhere???
__________________
trace("Good bye Flash.") Log.i(TAG, "Hello Droid");
Flash Gordon is offline   Reply With Quote
Old 12-11-2005, 01:24 PM   #7
senocular
six eyes
 
senocular's Avatar
 
Join Date: Jan 2003
Location: San Francisco, CA (USA)
Posts: 7,875
Send a message via ICQ to senocular Send a message via AIM to senocular Send a message via MSN to senocular Send a message via Yahoo to senocular
Default

try:
http://www.kirupa.com/developer/oop/index.htm
__________________
(6)
senocular is offline   Reply With Quote
Old 12-11-2005, 07:50 PM   #8
CyanBlue
Super Moderator
 
CyanBlue's Avatar
 
Join Date: Jan 2002
Location: Centreville, VA
Posts: 26,666
Default

I am only wishing somebody can actaully carve that idea into my brain... My brain is just not equipped to handle OOP... It only goes OOPS...
__________________
CyanBlue / Jason Je / Macromedia Certified Flash Developer & Designer
http://CyanBlue.FlashVacuum.com
http://www.FlashVacuum.com
http://tutorials.FlashVacuum.com

Do NOT PM, Email or Call me... Your question belongs right in this forum...
CyanBlue is offline   Reply With Quote
Old 12-11-2005, 08:00 PM   #9
senocular
six eyes
 
senocular's Avatar
 
Join Date: Jan 2003
Location: San Francisco, CA (USA)
Posts: 7,875
Send a message via ICQ to senocular Send a message via AIM to senocular Send a message via MSN to senocular Send a message via Yahoo to senocular
Default

You need to stop thinking that CB. I think you are your worst enemy when it comes to learning this stuff. It's really, at its core, not hard to understand at all and you are more than capable of understanding it.
__________________
(6)
senocular is offline   Reply With Quote
Old 12-12-2005, 12:50 PM   #10
CyanBlue
Super Moderator
 
CyanBlue's Avatar
 
Join Date: Jan 2002
Location: Centreville, VA
Posts: 26,666
Default

Yeah... I know... I've got to get that thought out of my brain and it is not that easy to do...
As I have said earlier, I think I sorta understand the concept of the OOP but I just cannot program in that manner(when I say in that manner, I am sorta talking about the AS2 class structure as well)... That's where the most of my problem lies, I guess...
__________________
CyanBlue / Jason Je / Macromedia Certified Flash Developer & Designer
http://CyanBlue.FlashVacuum.com
http://www.FlashVacuum.com
http://tutorials.FlashVacuum.com

Do NOT PM, Email or Call me... Your question belongs right in this forum...
CyanBlue is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT. The time now is 06:04 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.