OOP in Java. Script D

Содержание

Слайд 2

Agenda

Custom objects
Constructors
Context and "this"
Operator "new"

Agenda Custom objects Constructors Context and "this" Operator "new"

Слайд 3

Custom Object

Custom Object

Слайд 4

Object creation

You know that we can create a simple object in JavaScript.

Object creation You know that we can create a simple object in
We use JSON for this.

var cat = {
name: “Znizhok”,
color: “white”
};

[1]

Слайд 5

Object or Hash Table

But this way it looks like hash table creation.

Object or Hash Table But this way it looks like hash table
What is the difference between hash table and object, then?

var hash = {
key: value,
key: value
};

var object = {
key: value,
key: value
};

[1]

Слайд 6

Object or Hash Table

Typically we use hash table if we want to

Object or Hash Table Typically we use hash table if we want
represent some collection, and we use an object to describe some system or entity.

var cats = {
first: murzyk,
second: barsyk
};

var cat = {
name: barsik,
color: white
};

[1]

Слайд 7

Difference in use

There are some differences in using of hash tables and

Difference in use There are some differences in using of hash tables
objects as a result. For example:

cats["first"]; // good way

cat["name"]; // incorrect!
cat.name; // good way

To access elements of hash table we use indexer [ ] with key inside. But it's incorrect for objects! For objects Operator "." should be used :

[1]

[2]

Слайд 8

Constructors

Constructors

Слайд 9

Constructors

Sometimes we need to create more than one single object. It is

Constructors Sometimes we need to create more than one single object. It
not a good idea to use the literal way for this. It will be better create a scenario for objects reproducing.
Constructor is a function that implements this scenario in JavaScript.
Constructor consists of declaration attributes and methods that should be added into each new object with presented structure.

Слайд 10

Constructors: example

function Cat (name) {
this.name = name;
this.run = function ()

Constructors: example function Cat (name) { this.name = name; this.run = function
{
console.log(this.name + " run!");
};
return this;
}

var murzyk = new Cat("Murzyk");

[1]

[2]

Слайд 11

Context and "this"

Context and "this"

Слайд 12

Context

Let's imagine two identical objects.
They are created by Cat constructor:

var

Context Let's imagine two identical objects. They are created by Cat constructor:
murzyk = new Cat("Murzyk"),
barsyk = new Cat("Barsyk");

[1]

Слайд 13

Context

If we call method run() for both cats, we’ll take correct results:

murzyk.run();

Context If we call method run() for both cats, we’ll take correct

barzyk.run();

In console:
Murzyk run!

In console:
Barsyk run!

How does the interpreter distinguish whose name should be printed?

[1]

Слайд 14

Context

It works because we use the next form of access to attribute

Context It works because we use the next form of access to
name: this.name.
this contains inside a reference to object on whose behalf was called method run.
Such a reference is called a context.
The context determined automatically after the method calling and can't be changed by code.

Слайд 15

Loss of context

Be careful! There are situations when you can lose a

Loss of context Be careful! There are situations when you can lose
context. For example:

setTimeout(murzyk.run, delay);

In console:
undefined run!

murzyk.run is a reference to method. And only reference was saved in setTimeout. When the method was called by saved reference, object window will be used as a context and this.name (equal to window.name) was not found.

[1]

Слайд 16

Operator new

Operator new

Слайд 17

Pre-example

Imagine that some abstract factory produces cars. All cars are absolutely identical:

[1]

Pre-example Imagine that some abstract factory produces cars. All cars are absolutely identical: [1]

Слайд 18

Pre-example

But there are some emergency services and each of them has an

Pre-example But there are some emergency services and each of them has
own color scheme for a car:

[1]

Слайд 19

New: scenario of work

new processing has a similar scenario:
creation of default

New: scenario of work new processing has a similar scenario: creation of
object
calling of constructor with just created object context
modification of default object
returning and saving the reference to modified object

[1]

[2]

[3]

[4]

[5]

Слайд 20

New: example

creation of default object

var murzyk = new Cat("Murzyk");

var _temporary_ref =

New: example creation of default object var murzyk = new Cat("Murzyk"); var
new Object();

Interpreter creates some variables for temporary storing of reference to new object. Now it's a default object.

[1]

[2]

[3]

Слайд 21

New: example

calling of constructor with just created object context

var murzyk =

New: example calling of constructor with just created object context var murzyk
new Cat("Murzyk");

_temporary_ref.Cat();

_temporary_ref set as a context for constructor Cat.
this inside the Cat refers to as yet default object.

[1]

[2]

[3]

Слайд 22

New: example

modification of default object

var murzyk = new Cat("Murzyk");

this.name =

New: example modification of default object var murzyk = new Cat("Murzyk"); this.name
"Murzyk";
this.run = function () { . . . };

Interpreter extends the default object inside the constructor. If a key is not found, it will be created, as it occurs with hashes and arrays.

[1]

[2]

[3]

Слайд 23

New: example

returning and saving the reference to modified object

var murzyk = new

New: example returning and saving the reference to modified object var murzyk
Cat(“Murzyk”);

var murzik = _temporary_ref;

At last the reference to modified object returned and saved in a user variable.

[1]

[2]

[3]

Имя файла: OOP-in-Java.-Script-D.pptx
Количество просмотров: 136
Количество скачиваний: 0