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

More Related Content

PPT
Backbonejs
PDF
构建微信公众平台应用
PDF
Javascript Design Patterns
PDF
Contoh Factory pattern
PPTX
Chapter 3 - part1
PPTX
Object Oriented JavaScript
PDF
Js objects
PDF
JavaScript Inheritance
Backbonejs
构建微信公众平台应用
Javascript Design Patterns
Contoh Factory pattern
Chapter 3 - part1
Object Oriented JavaScript
Js objects
JavaScript Inheritance

Similar to Javascript Oop (20)

PDF
Intro to Ember.js
PDF
Java script object model
PDF
Design patterns in javascript
PDF
Migrating to Angular 2
ODP
Adding To the Leaf Pile
PPTX
MVC Puree - Approaches to MVC with Umbraco
PDF
03 objective-c session 3
PPTX
Class and Object in java core programming
PDF
Object Oriented Programming in JavaScript
PPT
Advanced Javascript
PPT
Advanced Javascript
PPT
Advanced Javascript
PDF
JavaScript Prototype and Module Pattern
PDF
Exercises of java tutoring -version1
PPT
Object Oriented JavaScript
PPT
Advanced Silverlight
PPTX
Oop objects_classes
PDF
Polymer - pleasant client-side programming with web components
PPT
Diving in the Flex Data Binding Waters
PDF
Angularjs - Unit testing introduction
Intro to Ember.js
Java script object model
Design patterns in javascript
Migrating to Angular 2
Adding To the Leaf Pile
MVC Puree - Approaches to MVC with Umbraco
03 objective-c session 3
Class and Object in java core programming
Object Oriented Programming in JavaScript
Advanced Javascript
Advanced Javascript
Advanced Javascript
JavaScript Prototype and Module Pattern
Exercises of java tutoring -version1
Object Oriented JavaScript
Advanced Silverlight
Oop objects_classes
Polymer - pleasant client-side programming with web components
Diving in the Flex Data Binding Waters
Angularjs - Unit testing introduction
Ad

More from mussawir20 (20)

PPT
Php Operators N Controllers
PPT
Php Calling Operators
PPT
Database Design Process
PPT
Php Simple Xml
PPT
Php String And Regular Expressions
PPT
Php Sq Lite
PPT
Php Sessoins N Cookies
PPT
Php Rss
PPT
Php Reusing Code And Writing Functions
PPT
Php Oop
PPT
Php My Sql
PPT
Php File Operations
PPT
Php Error Handling
PPT
Php Crash Course
PPT
Php Basic Security
PPT
Php Using Arrays
PPT
PPT
Javascript
PPT
Object Range
PPT
Prototype Utility Methods(1)
Php Operators N Controllers
Php Calling Operators
Database Design Process
Php Simple Xml
Php String And Regular Expressions
Php Sq Lite
Php Sessoins N Cookies
Php Rss
Php Reusing Code And Writing Functions
Php Oop
Php My Sql
Php File Operations
Php Error Handling
Php Crash Course
Php Basic Security
Php Using Arrays
Javascript
Object Range
Prototype Utility Methods(1)
Ad

Recently uploaded (20)

PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Cloud computing and distributed systems.
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Big Data Technologies - Introduction.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Spectroscopy.pptx food analysis technology
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Machine Learning_overview_presentation.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Encapsulation theory and applications.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Review of recent advances in non-invasive hemoglobin estimation
Agricultural_Statistics_at_a_Glance_2022_0.pdf
MIND Revenue Release Quarter 2 2025 Press Release
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Spectral efficient network and resource selection model in 5G networks
Dropbox Q2 2025 Financial Results & Investor Presentation
Cloud computing and distributed systems.
Machine learning based COVID-19 study performance prediction
Big Data Technologies - Introduction.pptx
The AUB Centre for AI in Media Proposal.docx
Spectroscopy.pptx food analysis technology
Unlocking AI with Model Context Protocol (MCP)
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Machine Learning_overview_presentation.pptx
MYSQL Presentation for SQL database connectivity
Encapsulation theory and applications.pdf
Chapter 3 Spatial Domain Image Processing.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Network Security Unit 5.pdf for BCA BBA.

Javascript Oop

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