A Flash Developer Resource Site

Results 1 to 13 of 13

Thread: .prototype and _proto ??

  1. #1

    Thumbs down

    hi there,

    If it's not to much boring, could someone spend a few time to explain me the function of .prototype and _proto, how to use it and in which case, because i don't really figure what's theire rule and don't understand the explanation in the manual.

    Thanks in advance.

  2. #2
    hi patadonf,
    where did you find this 'function'?
    some people call the constructor for a self-defined object prototype.

    function Book(name, price)
    {
    this.name = name;
    this.price = price;
    }
    book1 = new Book("Confederacy of Dunces", 19.95);
    book2 = new Book("The Floating Opera", 10.95);

  3. #3

    that's a part of it...

    but after defining the object, they re-use the methods of this object in an other using .prototype or _proto. And i don't really understand how it works.
    I need a very simple exemple to understand the difference beetween .prototype and _proto, and the advantage using such a propertie.

    thanks


  4. #4
    if i'm right, flash uses '_proto' intern to handel its object-model. i never had any problems with my objects in flash and i never used this property.
    '.prototype' is only used, to attach a method to a selfdefined object and all its instances.

    i will try to discover this and the advantage it may have. if i know something new i will inform you, but perhaps someone who knows more wil post to this thread.

  5. #5
    Senior Member
    Join Date
    Jun 2000
    Posts
    911
    You both have the right idea so I do not get where you are getting mixed up. You use "prototype" to attach a method to an object. For example, lets say I was going to make a constructor like this:
    Code:
    function vector_type (vx, vy, vz)
    {
          this.x = vx;
          this.y = vy;
          this.z = vz;
    }
    I could then create "vector_type"(s) like this:
    Code:
    v1 = new vector_type (3, 4, 5);
    v2 = new vector_type (20, 54, 12);
    Although creating objects like this is better then some other ways of programming there are still some more things you can add to make it even better. In the case of vectors, what are some things you can do with them (what kind of operations)? You can find a vector's length (norm) by using the pythagoream theorem, you can add two vectors, and "multiply" two vectors by the cross product (the dot product is also like multiplying but is not a good example here). So, we can make some functions to do these things:
    Code:
    // return a vector's length
    function vector_type_norm ()
    {
          return Math.sqrt (this.x*this.x + this.y*this.y + this.z*this.z);
    }
    
    // add two vectors
    function vector_type_addition (v1, v2)
    {
          this.x = v1.x + v2.x;
          this.y = v1.y + v2.y;
          this.z = v1.z + v2.z;
    }
    
    // find the cross product of two vectors
    function vector_type_cross (v1, v2)
    {
          this.x = v1.y * v2.z - v1.z * v2.y;
          this.y = v1.z * v2.x - v1.x * v2.z;
          this.z = v1.x * v2.y - v1.y * v2.x;
    }
    That may look a little wierd because of the "this" thrown around. We will get to that in a minute. Now, to attach those functions (methods) to an object you use "prototype" :
    Code:
    vector_type.prototype.norm = vector_type_norm;
    vector_type.prototype.addition = vector_type_addition;
    vector_type.prototype.cross_product = vector_type_cross;
    So, the above lines are literally attaching the functions we defined before hand to the "vector_type" object. The name on the right side of "prototype" is the new function name (the one you will be using later on) and the name on the right side of the equals sign is the function you are attaching. When attaching a function to an object you are giving that function complete access to the object's properties which is why you use "this". Here is an example of using the above stuff (assuming the above has already been implemented) :
    Code:
    // initialize a few vectors
    v1 = new vector_type (4, 2, -1);
    v2 = new vector_type (5, 3, 3);
    v3 = new vecotr_type ();
    
    // change "v3" to the sum of "v1" and "v2"
    v3.addition (v1, v2);
    
    // trace the values of vector "v3"
    trace (v3.x + "  " + v3.y + "  " + v3.z);
    
    // trace the norms of all the vectors
    trace (v1.norm ());
    trace (v2.norm ());
    trace (v3.norm ());
    
    // change "v3" to the cross product of "v1" and "v2"
    v3.cross_product (v1, v2);
    
    // trace the values of vector "v3"
    trace (v3.x + "  " + v3.y + "  " + v3.z);
    
    // trace "v3's" new norm
    trace (v3.norm ());
    All of those traces above should output something like this:
    Code:
    9  5  2
    4.58257569495584
    6.557438524302
    10.4880884817015
    9  -17  2
    19.3390796058137
    Now, hopefully you can see just how extremely useful all the above is. But pretty much everything I just posted is what I taught myself last night (literally)...so, I may be wrong on some of this stuff. The above works fine but I may be breaking some kind of fundamental OOP rule or something. Also since I am new to this I'm not exactly sure what all the terms are. I know that first function I gave is called a constructor. All the other functions are called methods and the functions as a whole are called a class (I think).

    When I was learning this stuff last night I couldn't go to bed because I wanted to redo everything this new way...its a lot better. Just in case you want another example here is a full vector class I wrote today. I can't upload *.as files to my host so I had to save the class a a *.txt file...on top of that I can't "hot link" to the text file so I have to link to an html file which has a link to the txt file (a bunch of crap I know). Just copy and paste the class into Flash, save as an *.as file, #include it in your files, and have fun.

    Good luck.

  6. #6
    hello ahab,
    thanks for your detailed posting. i realy know OOP, thats not the problem. i'm using objects in the most .fla-applications i build.
    but before i'd read patadonf's posting, i did't know something about the _proto-property.

    but i think your example could be very helpfull for anybody who is not so experianced in OOP.

    have a nice day :-)

  7. #7

    THANKS A LOT AHAB !!

    What for a great explanation !

    Now i well understand how use this function(or property... i also have little problems with terminology). That simplify the code by re-using it, and make it less confusing and much ligth.

    Your thread open me wild other doors in flash. One of them is using vectors... Cause i learned how use them years ago with my math teacher (the dear...), i don't remember how we can calculate movments with them.
    At this time i use simple 2D speed calculations with the difference between two points (ho ! isn't the value of a vector the result of this difference ?). But your 3D vector matrice seems to be very much powerfull, clear and compatible.

    don't you know a good tutorial about vectors, or haven't you got fla using the matrice to show it in application (not only for calculation but with objects in movment) ?

    best regards

  8. #8
    visit the location http://www.sfx.co.nz/tamahori/though...3d_howto.html.
    it is a tutorial for very strange director 3d-stuff, but the math should be the same. perhaps it will be interesting for you.

  9. #9

    thanks...

    the director syntax is confused for me... but someone send me this super work... have a look, it's a great source !
    http://users.bigpond.net.au/glc_design/glc3d.html

  10. #10
    proud new daddy! LuxFX's Avatar
    Join Date
    May 2000
    Location
    Dunsinane
    Posts
    1,148
    Excellent link patadonf. This is not only a good source of vector-3d code, but it also has a great example of using the __proto__ object!

    thanks!

  11. #11
    Senior Member
    Join Date
    Jun 2000
    Posts
    911
    I have actually written about vectors myself as well as 3d. Just check out the link in my signature. None of the code snippets I gave are object oriented because I only recently learned this technique but you should be able to understand the math.

    Good luck.

  12. #12
    proud new daddy! LuxFX's Avatar
    Join Date
    May 2000
    Location
    Dunsinane
    Posts
    1,148
    THANK YOU! I'm one of those people who, well, it's just been WAY too long since I had to do any real math, and I'm in need of a refresher. Your tutorials are exactly what I need. Thank you!!

  13. #13

    exactly what i need...

    a very-very helpfull tutorial !
    congratulations !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center