Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • JavaScript string prototype property

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 127
    Comment on it

    prototype property in JavaScript: The prototype property gives us freedom to add the new property and method into the object at any time. This is the global property in JavaScript which can be called through any JavaScript object at any time.

    Example of prototype property : Here I will show you how to use the prototype property with an example.

    1. <!DOCTYPE html>
    2. <html>
    3. <body>
    4.  
    5. <p id="demo"></p>
    6. <button onclick="show()">click</button>
    7. <script>
    8. employee = function(name, age) {
    9. this.name=name;
    10. this.age=age;
    11. }
    12. employee.prototype.salary = 2000;
    13. employee.prototype.display = function(){alert(this.salary)};
    14.  
    15. var emp = new employee("John", 26);
    16. document.getElementById("demo").innerHTML = emp.salary;
    17.  
    18. function show(){
    19. emp.display();
    20. }
    21. </script>
    22.  
    23. </body>
    24. </html>
    25.  
    26. Output : 2000

    And when you click on the click button it will show you the alert message with salary display on it. In the above example you can see that I have added one property called salary and one method called display after creating the employee object.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: