initClass(cls: Function, baseClass: Function|Array, name: String, interfaces: Array);
cls. Class to be initialized.
baseClass. Parent class or an array of such classes.
name. Full name of the class.
interfaces. Array of implemented interfaces.
The initClass method initializes a class.
To execute the example, add a link to PP.js scenario file to HTML page. Create a new namespace, enumeration, interface, a class that implements this interface, and in this class determine a method that executes an arithmetic operation for two values:
// Determine a new namespace within PP PP.ArithmeticNamespace = {}; PP.initNamespace(PP.ArithmeticNamespace, "PP.ArithmeticNamespace"); // Determine interface with one calc method in the created namespace PP.ArithmeticNamespace.OperationInterface = function () { var operationInterface = PP.ArithmeticNamespace.OperationInterface; PP.initInterface(operationInterface, "OperationInterface"); operationInterface.calc = PP.ClassMembers.Method; }; // Initialize enumeration that contains arithmetic operations (PP.ArithmeticNamespace.Operations = function () { var operations = PP.ArithmeticNamespace.Operations; operations.Sum = "Sum"; operations.Multiply = "Multiply"; PP.initEnum(operations, "PP.ArithmeticNamespace.Operations"); })(); /* Create the Calculator class that inherits from the PP.Object class and implements the PP.ArithmeticNamespace.OperationInterface interface */ PP.ArithmeticNamespace.Calculator = function (settings) { PP.ArithmeticNamespace.Calculator.base.constructor.call(this, settings); }; PP.initClass(PP.ArithmeticNamespace.Calculator, PP.Object, "PP.ArithmeticNamespace.Calculator", PP.ArithmeticNamespace.OperationInterface); // Implement the calc method var calculatorPrototype = PP.ArithmeticNamespace.Calculator.prototype; calculatorPrototype.calc = function (a, b, operation) { var result; switch (operation) { case PP.ArithmeticNamespace.Operations.Sum: result = a + b; break; case PP.ArithmeticNamespace.Operations.Multiply: result = a * b; break; }; return result; }; //Create an instance of the PP.Calculator class var calculator = new PP.ArithmeticNamespace.Calculator(); // Get sum of two numbers var a = 56, b = 87; var sum = calculator.calc(a, b, PP.ArithmeticNamespace.Operations.Sum); console.log(a + " + " + b + " = " + sum); // Get multiply of two numbers var mult = calculator.calc(a, b, PP.ArithmeticNamespace.Operations.Multiply); console.log(a + " x " + b + " = " + mult);
Executing this example creates the ArithmeticNamespace namespace,Operations enumeration, OperationInterface interface, and the Calculator class that implements this interface. The calc method that executes arithmetic operations for two values is defined for this class. The results of the addition and multiplication operations are displayed to the browser console:
56 + 87 = 143
56 x 87 = 4872
See also: