• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » JScript » String » JavaScript Prototype
Next →
← Prev

JavaScript Prototype

By Dinesh Thakur

A JavaScript Prototype is an object from which other objects inherit properties. Every object has a prototype by default. An object in JavaScript is any unordered collection of key-value pairs. If it’s not a primitive (undefined, null, Boolean, number or string) its an object.

As with the other objects that have the JavaScript Prototype property, you can use it to add properties or methods to String objects on the page. The following code shows an example of JavaScript Prototype:

<html> 
  <body> 
   <script type="text/JavaScript"> 
   String.prototype.attitude="cool"; 
    var rightnow= new String("Joe"); 
    document.write("This string is "+rightnow.attitude); 
    </script> 
   </body> 
</html> 

Now the String object “Joe” has an attitude property of “cool”!

The Output of this code is: This string is cool
The JavaScript Prototype property is used when you have a class and you have already created a set of objects and later on, you need a new property in each of your objects.
Lets see an example of JavaScript Prototype property. First we create a class called karan with a property called Lname.

function Karan(Lname){       
this.Lname = Lname;       
this.getLname=function(){return this.Lname;};       
this.setLname=function(LnameArg)
{        this.Lname=LnameArg;       };      } 

Then we create a few objects of this class as follows.

var p1= new Karan("karan1"); 
var p2= new Karan("karan2"); 

Now Later on suppose you need to add a property ‘age’ for all karan’s objects. First way to modify the class and add a property called age to it. But what happen if the Karan class is declared in common file and is call by different javascript scrips. That the stage where the prototype property comes into existence.
We can add a property to all the instances of the class by writing the following lines of code.

var p1= new Karan("karan1");       
var p2= new Karan("karan2");       
Karan.prototype.age=29;       
console.log(p1.age);       
console.log(p2.age); 

You’ll also like:

  1. Function Declaration or Prototype in C
  2. Write a C++ program for prototype constructors.
  3. JavaScript String substring() Method
  4. JavaScript String Format
  5. JavaScript Strings Object
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

JavaScript Tutorials

JavaScript Tutorials

  • JavaScript - Home
  • JavaScript - Operators
  • JavaScript - Comments
  • JavaScript - If...Else
  • JavaScript - For Loop
  • JavaScript - Popup Boxes
  • JavaScript - Switch Statement
  • JavaScript - while Loop
  • JavaScript - Functions
  • JavaScript - Variables
  • JavaScript - Statements
  • JavaScript - Substring()
  • JavaScript - indexOf()
  • JavaScript - Match()
  • JavaScript - Replace()
  • JavaScript - Search()
  • JavaScript - Split()
  • JavaScript - Substr()
  • JavaScript - Blink()
  • JavaScript - Anchor()
  • JavaScript - Big()
  • JavaScript - Strings Object
  • JavaScript - String Format
  • JavaScript - Length Property
  • JavaScript - Prototype
  • JavaScript - Slice Substr Vs Substring
  • JavaScript - JavaScript:void(0)

Other Links

  • JavaScript - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2023. All Rights Reserved.