note: for those SO fellow developers MS Ajax javascript library emulates classes, interface, enums and other OO features but the language does not support it. So don’t start bragging there isn’t a class in javascript.
Interface Implementation
/// <reference path="MicrosoftAjax.debug.js" />
Type.registerNamespace("Validators");
Validators.IValidate = function () {
throw Error.notImplemented("Interface IValidate must be implemented before invoke.");
};
Validators.IValidate.prototype = {
ErrorMessage: "",
IsValid: false,
Validate: function () {
throw Error.notImplemented("Class must provide a implementation for the method");
}
};
Validators.IValidate.registerInterface("Validators.IValidate");
Explanation:
Why am i explaining interface which doesn’t relate to the question? i will explain as it goes. The above code will create a Interface IValidate that mimics IValidator from the framework. The work of those Error.notImplemented code is as below
- Does not allow others to do this
new Validators.IValidate()(i.e) no instance - When a class implements the Interface,
Validate()method will throw a error when not implemented
So now you must have understood how the emulation is performed. Similarly i created a class (actually i wanted a abstract class that’s why i am here), named BaseValidator like this
Validators.BaseValidator = function () {
throw Error.invalidOperation("Base validator is a abstract class. Inherit the class to work with it");
};
Validators.BaseValidator.prototype = {
ValidationProperty: function (propertyName) {
},
BackColor: function (hexCode) {
},
ControlToValidate: function (id) {
},
CssClass: function (css) {
},
Display: function (mode) {
},
Focus: function () {
}
};
Validators.BaseValidator.registerClass("Validators.BaseValidator", null, Validators.IValidate);
at the last line you can notice that it implements Interface (but does not provide Implementation, because it will be provided by child classes of BaseValidator class). When ever a class implements Interface, inherits another class the constructor function should call Validators.BaseValidator.initializeBase(this) which would initialize base class. Now coming to the problem read carefully please
Problem
Validators.RequiredFieldValidator = function () {
Validators.RequiredFieldValidator.initializeBase(this, []);
};
Validators.RequiredFieldValidator.prototype = {};
Validators.RequiredFieldValidator.registerClass("Validators.RequiredFieldValidator", Validators.BaseValidator);
-
RequiredFieldValidator inherits BaseValidator as base class
-
In the constructor calls the
initializeBase(this)to initialize base class BUT
Constructor in the BaseClass throws a error.
Question
How am i supposed to emulate a abstract class with Microsoft Asp.net Ajax
Validators.RequiredFieldValidator = function () { };What’s wrong with simply not calling the super constructor. If the constructor your inheriting from is meant to be abstract (a useless thing to do) then simply don’t ever call it.