InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Javascript

    Javascript strings

    • Javascript - check if string is number
    • Javascript - parseInt
    • string ends with check
    • string starts with check

    Javascript Array

    • array forEach
    • array append item
    • array remove last item
    • array prepend item
    • array remove first item
    • create array from 0 to N-1
    • convert array to json string

    Javascript control flow

    • for-in loop to iterate over keys of javascript object

    Javascript DOM

    • Javascript - DOM appendChild example
    • Javascript - get computed style of an element
    • Javascript - how to view text inside DOM element
    • Javascript - img onload examples
    • Javascript - ready vs on load event handler example
    • Javascript - use querySelector to set dom element html
    • Javascript DOMContentLoaded listener example
    • Javascript event bubble vs capture
    • Javascript how to stop event propagation
    • Javascript onscroll event handler
    • Javascript onscroll event handler with throttling
    • Make an element draggable using Vanilla Javascript
    • Multiple onload handlers using vanilla Javascipt
    • Use universal selector to get all DOM nodes in vanilla Javascript
    • document querySelector examples
    • dump all handlers on window object

    Javascript Cookbook

    • Are Javascript functions objects?
    • Declare and invoke anonymous function
    • HTML5 drag and drop
    • JSLint on command line on Ubuntu
    • Javascript - call vs apply
    • Javascript - implement class using function
    • Javascript - iterate over function arguments
    • Javascript - print all methods of an object
    • Javascript - run a function at regular interval
    • Javascript - textarea and text input select all
    • Javascript arrow function examples
    • Javascript check if variable is defined
    • Javascript local and global variables
    • Javascript parse json string
    • Javascript prototype examples
    • Javascript settimeout example
    • Javascript sleep implementation
    • Requirejs - quickstart guide for beginners
    • catch errors using window.onerror
    • print javascript object to log

    Javascript libraries

    • AngularJS
    • CasperJS
    • PhantomJS
    • React
    • jQuery

    Javascript global functions

    • Javascript - Number function
     
    • Home
    • > Tutorials
    • > Javascript

    Javascript prototype examples

    on Feb 21, 2016

    Javascript function can be used to define classes. Inside classes, member properties (containing functions, objects, etc.) can be defined either as member variables in constructor or using function prototype members. Some comments on using Javascript prototype

    1. Prototype member property is shared by all instance of the class. Hence new instance does not recreate the method.
    2. Changing the prototype method, changes it for all instances.
    3. When a property is not found in instance of class, it is looked up in prototype. If same property is defined in instance and prototype, one in instance is used.

    Defining function in constructor example

    Here we are defining member function in constructor which will create separate instance of function with each class instance. The counter count set in function will not be shared among instances.

    <script type="text/javascript">
    function testclass() {
      this.v1 = "value1"
      this.f1 = function () {
        arguments.callee.count = arguments.callee.count || 0;
        arguments.callee.count++;
        document.write("function counter: " + arguments.callee.count + "<br/>")
      }
    }
    var o1 = new testclass();
    var o2 = new testclass();
    if (o1.f1 === o2.f1) {
      document.write("o1.f1 and o2.f1 are same <br/>");
    } else {
      document.write("o1.f1 and o2.f1 are not same <br/>");
    }
    o1.f1()
    o2.f1()
    </script>
    refresh done
    try it online

    Defining function in prototype example

    Here we are defining member function in prototype which will use same instance of function with each class instance. The counter count set in function will be shared among all instances. Unless there is a specific reason, it is better to set functions in prototype.

    <script type="text/javascript">
    function testclass() {
      this.v1 = "value1"
    }
    testclass.prototype.f1 = function () {
      arguments.callee.count = arguments.callee.count || 0;
      arguments.callee.count++;
      document.write("function counter: " + arguments.callee.count + "<br/>")
    }
    var o1 = new testclass();
    var o2 = new testclass();
    if (o1.f1 === o2.f1) {
      document.write("o1.f1 and o2.f1 are same <br/>");
    } else {
      document.write("o1.f1 and o2.f1 are not same <br/>");
    }
    o1.f1()
    o2.f1()
    </script>
    refresh done
    try it online

    Suggested posts:

    1. Are Javascript functions objects?
    2. Css position property – static, relative, absolute and fixed positioning
    3. WordPress – prevent access to php files in wp-includes
    4. Javascript – call vs apply
    5. PHP – get function arguments examples
    6. Javascript – create array from 0 to N-1 in nodejs
    7. AngularJS controller basic clock example
    8. HTML5 canvas clear rectangle example
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged Javascript, Javascript cookbook, Tutorials
    • Browse content
    • Article Topics
    • Article archives
    • Contact Us
    Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

    Copyright © 2025 InfoHeap.

    Powered by WordPress