HomeLanguagesJavascriptJavaScript String prototype Property

JavaScript String prototype Property

The prototype property allows to add new properties and methods to the existing JavaScript object types. There are two examples to describe the JavaScript String prototype property.

Syntax:

object.prototype.name = value

Return Value: It returns a reference to the String.prototype object.

Example 1: This example adds a property salary to the all objects.




<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        JavaScript String prototype Property
    </title>
</head>
  
<body style="text-align:center;">
  
    <h1>GeeksForGeeks</h1>
      
    <p id="GFG_UP"></p>
  
    <button onclick="GFG_Fun();">
        Click Here
    </button>
      
    <p id="GFG_DOWN"></p>
      
    <script>
        var up = document.getElementById('GFG_UP');
        var down = document.getElementById('GFG_DOWN');
          
        function person(name, age) {
            this.name = name;
            this.age = age;
        }
        var p1 = new person("p1", 24);
        up.innerHTML = "Click on the button to add "
                + "a new property to all objects of"
                + " a given type.< br > " 
                + JSON.stringify(p1);
  
        function GFG_Fun() {
            person.prototype.salary = 1000;
            down.innerHTML = p1.salary;
        }
    </script>
</body>
  
</html> 


Output:

Example 2: This example adds a method info to the all objects.




<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        JavaScript String prototype Property
    </title>
</head>
  
<body style="text-align:center;">
  
    <h1>GeeksForGeeks</h1>
  
    <p id="GFG_UP"></p>
  
    <button onclick="GFG_Fun();">
        Click Here
    </button>
      
    <p id="GFG_DOWN"></p>
      
    <script>
        var up = document.getElementById('GFG_UP');
        var down = document.getElementById('GFG_DOWN');
        function person(name, age) {
            this.name = name;
            this.age = age;
        }
        var p1 = new person("p1", 22);
        up.innerHTML = "Click on the button to add"
                + " a new property to all objects "
                + "of a given type.< br > " 
                + JSON.stringify(p1);
  
        function getData(name, age) {
            return "Information of person <br>Name - '" 
                        + name + "'<br>Age - " + age;
        }
  
        function GFG_Fun() {
            person.prototype.info 
                    = getData(p1.name, p1.age);
                      
            down.innerHTML = p1.info;
        }
    </script>
</body>
  
</html> 


Output:

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

5 COMMENTS

Most Popular

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS