even more private methods on prototype

JavaScript performance comparison

Test case created

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
my method
function createDoctor(name, specialty, mySallary) {
  return (function(name, specialty, sallary) {

    /*var sallary = sallary,
         // private variables: they will persist
        name = name,
        specialty = specialty;*/


    function Doctor() {};

    Doctor.prototype = (function() {
      return {
        setSpecialty: function(value) {
          specialty = value;
        },
        getSpecialty: function() {
          return specialty;
        },
        setName: function(value) {
          name = value;
        },
        getName: function() {
          return name;
        },
        // methods to set and get outer private variables
        getSallary: function() {
          return sallary;
        },
        setSallary: function(value) {
          sallary = value;
        }
      };
    }());

    return new Doctor();
  }(name, specialty, mySallary));
}
var manos = createDoctor("Manos", "Gynae", 10000);
manos.setSallary(300000);
manos.getName() + " " + manos.getSallary();

var panos = createDoctor("Pano", "xxx", 0);
panos.setName("Panos");
panos.setSpecialty("Mathematician");
panos.setSallary(10);
panos.getName() + " " + panos.getSallary() + " " + panos.getSpecialty();
manos.getName() + " " + manos.getSallary();
pending…
Crockford's metnod
function Doctor(name, specialty, sallary) {
  this.name = name;
  this.specialty = specialty;

  var sallary = sallary
  this.getSallary = function() {
      return sallary;
      }
     
  this.setSallary = function(value) {
    sallary = value;
  }
}
Doctor.prototype = (function() {
  return {
    // public attributes and methods
    setName: function(name) {
      this.name = name;
    },
    getName: function() {
      return this.name;
    },
    setSpecialty: function(value) {
      this.specialty = value;
    },
    getSpecialty: function() {
      return this.specialty;
    }
  };

}());

var manos = new Doctor("Manos", "Gynae", 10000);
manos.setSallary(300000);
manos.getName() + " " + manos.getSallary();

var panos = new Doctor("Pano", "xxx", 0);
panos.setName("Panos");
panos.setSpecialty("Mathematician");
panos.setSallary(10);
panos.getName() + " " + panos.getSallary() + " " + panos.getSpecialty();
manos.getName() + " " + manos.getSallary();
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment