javascript dynamic classes code example

Example: javascript dynamic classes

JavaScript "classes", just like any other object, can be dynamically created. So, yes, this can be done.

You would do something like this in the code handling the AJAX response (assuming that the AJAX response was providing the name of the new "class", and it's in a variable called newClassName):

window[newClassName] = function() {
  // New class name constructor code
}

window[newClassName].prototype = {

  someProperty: "someValue",

  someMethod: function(a, b) {
  },

  someOtherMethod: function(x) {
  }
}

Tags:

Misc Example