Class in the JavaScript it is a constructor function | стандарт ES5
last updated: 31 July 2020
Class is
constructor function , so it is
Normal function in JavaScript...
// определяем класс (функция-коснструктор)
function MyBook (name)
{
...
// BookName это внутренняя переменная
var BookName = ;
// Price это внешняя переменная
this .Price = 0;
...
}
A class (constructor function) can contain:
• internal variables: text, numbers, flags, date, array, property set
• external variables: text, numbers, flags, date, array, property set
• internal functions
• External links
• static functions
Example
Html
<html>
<body>
<!-- text on the page -->
<h1> Hello!</h1>
<script language='JavaScript'>
// define a class MyBook
function MyBook (name)
{
// BookName it's an internal variable
// BookName NOT visible from the outside because it is declared as var
var BookName = name;
// Author , Price , Valuta these are external variable functions MyBook
// these variables are visible from the outside because they are declared as this.
this .Author = "";
this .Price = 0;
this .Valuta = '';
// MyInfo it is an external variable of the function MyBook with a set of properties
// MyInfo is visible from the outside because it is declared as this.
this .MyInfo =
{
PagesCount: 90,
HasPictures: true
};
// Shops it is an external variable of the function MyBook contains an empty array
// Shops is visible from the outside because it is declared as this.
this .Shops = []; // shops where you can buy books
// ShowBookName it is an external function-variable with no parameters
// ShowBookName is visible from the outside because it is declared as this.
this .ShowBookName = function ()
{
alert (BookName);
}
// ShowBookShops it is an external variable function with a parameter title
// ShowBookShops is visible from the outside because it is declared as this.
this .ShowBookShops = function (title)
{
alert (title + ": " + this .Shops);
this .MyFuncHello ();
}
// MyInnerFunc1 is an internal variable function
// MyInnerFunc1 NOT visible from the outside because it is declared as var
var MyInnerFunc1 = function ()
{
alert ("Hello inner function 1" );
}
// MyInnerFunc1 it is an internal function
// MyInnerFunc1 NOT visible from the outside because, declared inside the function MyBook
function MyInnerFunc2 ()
{
alert ("Hello inner function 2" );
}
}
// MyStaticFunc it is a static function
// MyStaticFunc is visible from the outside and is used only with the name MyBook
MyBook.MyStaticFunc = function ()
{
alert ("Hello static function" );
}
// MyFuncHello it is an internal function
// MyFuncHello NOT visible from the outside because, declared inside the function MyBook
MyBook.prototype.MyFuncHello = function ()
{
alert ("Hello function" );
}
// To create a class
var book = new MyBook ("Vlastelin kolec" );
// setting values to external variables
book.Author = "Nik Perumov" ;
book.Price = 60;
book.Valuta = "Rubles" ;
book.Shops = ["Minsk" , "Moscow" , "London" ];
// calling an external variable function
book.ShowBookName();
// на экране увидим "Vlastelin kolec"
// calling an external variable function
book.ShowBookShops("Shops" );
// on the screen we will see "Shops: Minsk, Moscow, London"
// call a static function
MyBook.MyStaticFunc();
// на экране увидим "Hello static function"
// call an internal function
book.MyInnerFunc1(); error: 'book.MyInnerFunc1 is not a function'
</script>
</body>
</html>
Creating an Object
The object is created through new and function name.
JavaScript
Example
var book = new MyBook ();
Internal and external variables
MyBook contains an internal variable BookName because, declared as var
MyBook contains external variables Author , Price , Valuta , MyInfo , Shops because, declared as this.
JavaScript
// class MyBook
function MyBook (name)
{
...
// internal variable
var BookName = name;
// external variables
this .Author = "";
this .Price = 0;
this .Valuta = '';
this .MyInfo =
{
PagesCount: 90,
HasPictures: true
};
this .Shops = [];
...
}
this. indicates that an external variable has been declared for the class MyBook .
This variable will live as long as the object MyBook .
The internal variable is declared as var BookName
will collapse immediately after exiting the function MyBook
На заметку! Переменная BookName используется внутрии функции ShowBookName .
Поэтому переменная BookName будет жить столько же сколько функция ShowBookName .
А функция ShowBookName живет столько же сколько и функция MyBook .
То есть переменная BookName будет жить столько же сколько функция MyBook
Internal and external functions
Inside MyBook we have announced a feature with the name ShowMyMessage with parameter title
JavaScript
// class MyBook
function MyBook (name)
{
...
// function
this .ShowMyMessage = function (title )
{
alert (title + " " + this .Name + " " + this .MyInfo.PagesCount);
}
...
}
Статические функции