Monday, 30 September 2013

Extending properties/methods of an existing class in JavaScript

In JavaScript, properties/methods of an existing class can be extended by using the 'prototype' property of that class. Let's see an example of how to add a function named 'isInt' to String class. This function, as is obvious from its name, tells whether the text content of the target String is a number or not.


<script>
if (String.prototype.isInt == null) {
String.prototype.isInt = function() {
var result = parseInt(this.valueOf());
return !isNaN(result);
}
}
var i = "27";
var j = "shyam";
var paraElement = document.getElementById("output");
paraElement.innerHTML = ("Is '27' an integer : " + i.isInt());
paraElement.innerHTML += (" <br > Is 'shyam' an integer : " + j.isInt());
</script>

3 comments:

  1. Right explanation right example :)

    ReplyDelete
  2. perfect explanations with out errors i like this blog ever
    if you want to choose best person as a life partner in your life can you must and should read below article carefully
    Title:Kamma Kalyanam Matrimony
    Description:
    KAMMA'S MOST REPUTATED AND TRUSTED MATRIMONIAL PORTAL
    Kamma's Marriages is the solution for delivering reliable and quality match making services to all the briders and grooms seeing to enter into marital bliss.We arrange alliances through our extensive database to match your needs and tates. Since 1982, we have been very successful in providing alliances and settling marriages to thousands of our valued customers and commited to discover your better half!Kammakalyanam is a platform of bringing in the prospective brides and bridegrooms to the heaven of marriage with its extensive database across all economic strata.

    Website:https://kammakalyanam.com/
    Name:Kamma Kalyanam Matrimony
    Keywords:Kamma matrimony, Kamma marriage bureau, kamma brides, kamma grooms Matrimony, Wedding
    Address: MIG 165 SMR heights 5 th floor Opp to Holistic Hospitals, no 1, KPHB Road, Hyderabad, Telangana 500072
    EMAIL: INFO@TELUGUMARRIAGES.COM

    ReplyDelete