JAVASCRIPT OOP
Introduction <ul><li>//adding a custom property to a prebuilt object var myimage=new Image()  myimage.size=&quot;26k&quot;...
Using the prototype object to add custom properties to objects <ul><li>//First, create the custom object &quot;circle&quot...
Using the prototype object to add custom methods to objects <ul><li>//First, create the custom object &quot;circle&quot; f...
Example  1-Extending functionality to the pre-built string() object   <ul><li><script type=&quot;text/javascript&quot;> /*...
Example 2 Extending functionality to a custom JavaScript object   <ul><li><script type=&quot;text/javascript&quot;> //crea...
Upcoming SlideShare
Loading in …5
×

Javascript Oop

873 views
781 views

Published on

OOP in javascript small introduction

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

  • Be the first to like this

No Downloads
Views
Total views
873
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Javascript Oop

  1. 1. JAVASCRIPT OOP
  2. 2. Introduction <ul><li>//adding a custom property to a prebuilt object var myimage=new Image() myimage.size=&quot;26k&quot; /*adding a custom property to the custom object &quot;circle&quot;*/ //First, create the custom object &quot;circle&quot; function circle(){ } </li></ul><ul><li>var smallcircle=new circle() </li></ul><ul><li>smallcircle.pi=3.14159 </li></ul><ul><li>It will not work when a new object will created from circle as not inherited to it. </li></ul>
  3. 3. Using the prototype object to add custom properties to objects <ul><li>//First, create the custom object &quot;circle&quot; function circle(){ } circle.prototype.pi=3.14159 </li></ul>This is javascript object that helps function to inherit new properties and methods
  4. 4. Using the prototype object to add custom methods to objects <ul><li>//First, create the custom object &quot;circle&quot; function circle(){ } circle.prototype.pi=3.14159 // create the object method. </li></ul><ul><li>function alertmessage(){ alert(this.pi) } circle.prototype.alertpi=alertmessage </li></ul>
  5. 5. Example 1-Extending functionality to the pre-built string() object <ul><li><script type=&quot;text/javascript&quot;> /*code for extending String object with method that writes text backwards*/ </li></ul><ul><li>//core custom method for writing text backwards </li></ul><ul><li>function outputbackwards(){ for (i=this.length-1;i>=0;i--) document.write(this.charAt(i)) </li></ul><ul><li>} </li></ul><ul><li>/Attach custom method to string object String.prototype.writeback=outputbackwards </li></ul><ul><li>var message1=&quot;Welcome to my site!&quot; </li></ul><ul><li>message1.writeback() </li></ul><ul><li>var message2=&quot;Today is a beautiful day&quot; </li></ul><ul><li>message2.writeback() </li></ul><ul><li></script> </li></ul><ul><li>Output: </li></ul><ul><li>!etis ym ot emocleW yad lufituaeb a si yadoT </li></ul>
  6. 6. Example 2 Extending functionality to a custom JavaScript object <ul><li><script type=&quot;text/javascript&quot;> //create dummy object function dummy(){ } //Create custom property function dummyproperty(){ } //Create custom method function dummymethod(){ } dummy.prototype.prop=dummyproperty dummy.prototype.method=dummymethod </li></ul><ul><li></script> </li></ul>

×