Object-Oriented Programming (OOP) is a new and popular programming trend, is supported by almost the programming languages today, and JavaScript is not an exception, too. This post provides some basic concepts in using OOP in Javascript.
- Demo
- Enlarge
- Reload
- New window
Free iPage Web Hosting for First Year NOW
If you're still looking for a reliable web host provider with affordable rates, why you don't take a little of time to try iPage, only with $1.89/month, included $500+ Free Extra Credits for the payment of 24 months ($45)?
Over 1,000,000+ existisng customers can not be wrong, definitely you're not, too! More important, when you register the web hosting at iPage through our link, we're going to be happy for resending a full refund to you. That's awesome! You should try iPage web hosting for FREE now! And contact us for anything you need to know about iPage.
Object-Oriented Programming is currently one of the most prominent aspects of programming. Many programming courses teach OOP, and PHP5 supports proper OOP. While Javascript as a language is not inherently well-suited for full fledged OOP, it is possible to make it work.
Here is a basic primer in using OOP in Javascript.
Methods vs Objects
In Javascript, both general methods and objects are defined using the function
keyword. For example, the method and the object definitions below are both denoted by function
.
function add( a, b ) { return a+b; } function sum( a, b ) { this.total = a+b; }
The difference between a method call and an object instantiation call is the keyword new
. The difference is illustrated in the example below:
var addition = add( 1, 2 ); var summation = new sum( 1, 2 );
Fields
In OOP in Javascript, objects have fields that store data. You have already seen an example of this earlier in the sum
object, which stores the sum of a+b
as total
. All of these fields are public - that is to say, modifiable by elements other than the object itself. For example:
var summation = new sum( 1, 2 ); alert(summation.total); // shows 3 summation.total = 5; alert(summation.total); // shows 5
Methods in Objects
Next, objects can declare their own methods. These methods can be defined within the object definition, or outside of the definition and assigned to a variable within the object definition.
This provides an example of the former:
function sum( a, b ) { this.total = add( a, b ); this.print = function() { print(this.total); } }
and this provides an example of the latter:
function sum( a, b ) { this.total = add( a, b ); this.print = print; } function print( total ) { print(total); }
Inheritance
Finally, a major part of OOP is inheritance. Inheritance is when a "subclass" takes on all the methods of a "superclass". Inheritance in Javascript is done using two lines:
this.inheritFrom = ; this.inheritFrom();
Here is an example of the subclass retaining the methods of the superclass while adding methods of its own.
function super() { this.alert = function() { alert("Hello!"); } this.alert2 = function() { alert("Hello2!"); } } function sub() { this.inheritFrom = super; this.inheritFrom(); this.alert3 = function() { alert("Hello3!"); } } var c = new sub(); c.alert(); // alerts "Hello!" c2.alert2(); // alerts "Hello2!" c2.alert3(); // alerts "Hello3!"
While this primer is not necessarily in-depth, it has hopefully given you a basic understanding how OOP works in Javascript.
- Sent (0)
- New
Generate your business videos by AI with voice or just text
chatGPTaz.com
Talk to ChatGPT by your mother language
AppAIVideo
Your first FREE AI Video App
Deepfake Video
Deepfake AI Video Maker
Deepfake
Deepfake AI Video Maker
AI Deep Fake
Deepfake AI Video Maker
AIvidio
AI Video Mobile Solutions
AIvideos
AI Video Platform & Solutions
AIvedio
AI Video App Maker
Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE
Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE
Temu Free $500 for New Users
Claim Free Temu $500 Credit via Affiliate & Influencer Program
Free TikTok Ads Credit
Master TikTok Ads for Your Business Marketing
Dall-E-OpenAI.com
Generate creative images automatically with AI
chatGPT4.win
Talk to ChatGPT by your mother language
First AI Product from Elon Musk - Grok/UN.com
Speak to Grok AI Chatbot with Your Language
Tooly.win
Open tool hub for free to use by any one for every one with hundreds of tools
GateIO.gomymobi.com
Free Airdrops to Claim, Share Up to $150,000 per Project
iPhoneKer.com
Save up to 630$ when buy new iPhone 16
Buy Tesla Optimus Robot
Order Your Tesla Bot: Optimus Gen 2 Robot Today for less than $20k