📜 ⬆️ ⬇️

JsOOP

The article focuses on the imitation of classic OOP and strong typing in JavaScript (<2.0).

I’ll tell you about JsOOP , a mini-framework for writing JavaScript code in object-oriented style.

Let's start with OOP. There are many options to introduce the concept of class in JavaScript. I will not list them all, I suggest immediately to proceed to the definition of a class using JsOOP:
')
var Zoo = Zoo || {};

Zoo.Animal = Class
({
construct: function (name)
{
this ._name = name;
},

methods:
{
Scream: null ,

SayName: function ()
{
document .write(Zoo.Animal.QuoteName( this ._name));
}
},

statics:
{
QuoteName: function (name)
{
return "{ " + name + " }" ;
}
}
})


* This source code was highlighted with Source Code Highlighter .

First, the namespace of the Zoo is determined. Then the Animal class inside it:
construct is a constructor function, methods is a set of instance methods ( Scream is an abstract method), statics is a set of static methods.

It is worth mentioning that there is no support for fully-fledged private and protected fields and class methods in JsOOP. Therefore, a recommendation is introduced to begin the names of such variables and functions with an underscore.

Now we define the derived class:

Zoo.Dog = Class
({
base : Zoo.Animal,

construct: function (name, bark, loudness)
{
this .$ base ()(name);
this ._bark = bark;
this ._loudness = loudness;
},

methods:
{
Scream: function ()
{
var s = "" ;
for ( var i = 0; i < this ._loudness; i++)
{
s += this ._bark;
}
document .write(s);
},

SayName: function ()
{
document .write( this ._bark + " " );
this .$ base ( 'SayName' )();
}
}
})


* This source code was highlighted with Source Code Highlighter .

Notice how to call the constructor and method of the base class from the derived.

A simple example of using a class:

var bob = new Zoo.Dog( "Bob" , "Woof!" , 3);

bob.SayName(); // "Woof!Woof! { Bob }"
bob.Scream(); // "Woof!Woof!Woof!"


* This source code was highlighted with Source Code Highlighter .

Optionally, in the description of any class, you can add a section vars . For example, for the class Dog :

Zoo.Dog = Class
({
// ...

vars:
{
_bark: Type.String,
_loudness: Type.Number
}

// ...
})


* This source code was highlighted with Source Code Highlighter .

In the Type namespace, various types of variables are defined. These are actually predicates, for example:

Type.String = function (v)
{
return typeof (v) == "string" ;
}


* This source code was highlighted with Source Code Highlighter .

You can always extend this list, for example, with Type.Interface , which takes as its parameter an array of method names that the variable object should support. That is, the description of the class field looks like this:

vars:
{
_animal: Type.Interface([ 'SayName' , 'Scream' ])
}


* This source code was highlighted with Source Code Highlighter .

Download JsOOP here . Content:

oo_debug.js - supports class descriptions in the considered style ( Class function) and checks the types of variables from the vars section before and after executing the code of any class method.

oo.js is the same as the previous script, only type checking does not occur. The vars section is ignored.

types.js - descriptions of types of predicates.

Source: https://habr.com/ru/post/46414/


All Articles