Class

380 views

Published on

Prototype Javascript

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
380
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Class

  1. 1. Module Class
  2. 2. Class <ul><li>create() -> Function </li></ul><ul><li>Returns an function that acts like a Ruby class </li></ul><ul><li>Class.create() returns a function that, when called, will fire its own initialize method. This allows for more Ruby-like OOP. It also lets you more easily subclass by overriding a parent's constructor. </li></ul>
  3. 3. Class <ul><li>Example </li></ul><ul><li>var  Animal = Class.create(); </li></ul><ul><li>Animal.prototype = {  initialize:  function (name, sound) </li></ul><ul><li>  {    this.name  = name;    this.sound = sound;  },  </li></ul><ul><li>   speak:  function () {    alert(name +  &quot; says: &quot;  + sound +  &quot;!&quot; );  } </li></ul><ul><li> }; </li></ul><ul><li>var  snake =  new  Animal( &quot;Ringneck&quot; ,  &quot;hissssssssss&quot; ); </li></ul><ul><li>snake.speak();// -> alerts &quot;Ringneck says: hissssssssss!“ </li></ul><ul><li>  var  Dog = Class.create(); Dog.prototype = Object.extend( new  Animal(), {  </li></ul><ul><li>  initialize:  function (name) {    this.name  = name; this.sound =  &quot;woof&quot; ;  }  }); </li></ul><ul><li>var  fido =  new  Dog( &quot;Fido&quot; ); </li></ul><ul><li>fido.speak(); </li></ul><ul><li>// -> alerts &quot;Fido says: woof!&quot; </li></ul>

×