Using objects in JavaScript. Accessing DOM in JavaScript презентация

Содержание


Презентации» Информатика» Using objects in JavaScript. Accessing DOM in JavaScript
Using objects in JavaScript. Accessing DOM in JavaScript
 Vyacheslav Koldovskyy LastAgenda
 Program flow control 
 Collections
 Custom objects
 Constructors
 Context andConditions: if-else
 Syntax:
 if (condition) 
   statement1  [elseConditional (Ternary) Operator ?:
 Syntax:
 condition ? expr1 : expr2 
Loops: forLoops: while and do-whileLoops: keywords break and continue 
 There are two keywords forSwitch
 Switch statement allows to select one of many blocks ofCollections
 Collection is a set of variables grouped under common name.
Array: processing
 Usage of arrays: 
  var array = []Array: features
 Arrays in JavaScript differ from arrays in classical languages.Array: useful methodsIterating an ArrayDictionary
 Dictionaries allow us to have set of data in formUsing Dictionary
 Usage of dictionaries tables is very similar to arrays:
Array vs Dictionary
 Use Array for collections with digital indexes.
 UseObject creation
 You know that we can create a simple objectObject or Dictionary
 But this way it looks like hash tableObject or Dictionary
 Typically we use hash table if we wantDifference in useConstructors
 Sometimes we need to create more than one single object.Constructors: exampleBOM and DOMDescription
 How JavaScript communicates with the world?
 In outline this mechanismEvent handling
 But JavaScript doesn't observe events by default. You shouldInline handlingUsing of onevent attributeProper waysProper waysBubbling and CapturingBubbling and CapturingEvent object
 For every event in the browser instance of EventControl of Default behavior
 Sometimes a default scenario of event processingSamplePractice TaskAdvancedContext
 Let's imagine two identical objects. 
 They are created byContext
 If we call method run() for both cats, we’ll takeContext
 It works because we use the next form of accessLoss of context
 Be careful! There are situations when you canBasic info
 Free space in browser sandbox is allocated for eachScope
 The scope is a special JavaScript object which was createdScopeValue-types and Reference-types
 Unfortunately some objects are too large for scope.Memory cleaning
 The basic idea of memory cleaning: when function isUnreachable links
 An object is considered unreachable if it is notUnreachable linksClosure
 If scope is an object and it is not deletedExampleContacts



Слайды и текст этой презентации
Слайд 1
Описание слайда:
Using objects in JavaScript. Accessing DOM in JavaScript Vyacheslav Koldovskyy Last update: 29/03/2016


Слайд 2
Описание слайда:
Agenda Program flow control Collections Custom objects Constructors Context and "this" Operator "new" Browser Object Model (BOM) and Document Object Model (DOM) Events Memory and Sandbox Closures

Слайд 3
Описание слайда:
Conditions: if-else Syntax: if (condition) statement1 [else statement2] Example:

Слайд 4
Описание слайда:
Conditional (Ternary) Operator ?: Syntax: condition ? expr1 : expr2 Example:

Слайд 5
Описание слайда:
Loops: for

Слайд 6
Описание слайда:
Loops: while and do-while

Слайд 7
Описание слайда:
Loops: keywords break and continue There are two keywords for loops control : break – aborts loop and moves control to next statement after the loop; continue – aborts current iteration and immediately starts next iteration. Try not to use this keywords. A good loop have one entering point, one condition and one exit.

Слайд 8
Описание слайда:
Switch Switch statement allows to select one of many blocks of code to be executed. If all options don’t fit, default statements will be processed

Слайд 9
Описание слайда:
Collections Collection is a set of variables grouped under common name. Usually elements of collections are grouped according to some logical or physical characteristic. Collections help to avoids situations when we have to declare multiple variables with similar names:: var a1, a2, a3, a4… There are two types of collections that are typical for JS: arrays and dictionaries (hash tables).

Слайд 10
Описание слайда:
Array: processing Usage of arrays: var array = [] // declaration of empty array var array = [5, 8, 16] // declaration of predefined array array[0] = 4; // writing value with index 0 tmp = array[2]; // reading value by index (in tmp - 16) array.length // getting length of array

Слайд 11
Описание слайда:
Array: features Arrays in JavaScript differ from arrays in classical languages. Arrays in JS are instances of Object. So Array in JS can be easily resized, can contain data of different types and have string as an index. Length of array is contained in length property, its value is equal to index of last element increased by one.

Слайд 12
Описание слайда:
Array: useful methods

Слайд 13
Описание слайда:
Iterating an Array

Слайд 14
Описание слайда:
Dictionary Dictionaries allow us to have set of data in form of "key-value" pairs We can create hash and initialize it at the same time. For this we should write values separated by a comma like in array. But for all values we have to set key:

Слайд 15
Описание слайда:
Using Dictionary Usage of dictionaries tables is very similar to arrays: dict['good'] = 4; // writing value in element with key “good” tmp = dict['excellent']; // reading value by key “excellent” The difference is in usage of for-in statement:

Слайд 16
Описание слайда:
Array vs Dictionary Use Array for collections with digital indexes. Use Hash if you want use string keys. Don't look for property length in Hash. Don't look for forEach and other Array methods in Hash. Always explicitly declare Array otherwise you get a Hash. Don't use for with hash, use for-in instead. At finally : use collection – be cool :)

Слайд 17
Описание слайда:
Object creation You know that we can create a simple object in JavaScript. We use JSON for this.

Слайд 18
Описание слайда:
Object or Dictionary But this way it looks like hash table creation. What is the difference between hash table and object, then?

Слайд 19
Описание слайда:
Object or Dictionary Typically we use hash table if we want to represent some collection, and we use an object to describe some system or entity.

Слайд 20
Описание слайда:
Difference in use

Слайд 21
Описание слайда:
Constructors Sometimes we need to create more than one single object. It is 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.

Слайд 22
Описание слайда:
Constructors: example

Слайд 23
Описание слайда:
BOM and DOM

Слайд 24
Описание слайда:
Description How JavaScript communicates with the world? In outline this mechanism works by next scenario: user does something and this action is an event for browser. JavaScript observes pages in the browser. And if event has occurred, script will be activated.

Слайд 25
Описание слайда:
Event handling But JavaScript doesn't observe events by default. You should specify to your code what events are interesting for it. There are 3 basic ways to subscribe to an event: - inline in HTML - using of onevent attribute using special methods First and second ways are deprecated for present days. Let's take a look at event handling in more details.

Слайд 26
Описание слайда:
Inline handling

Слайд 27
Описание слайда:
Using of onevent attribute

Слайд 28
Описание слайда:
Proper ways

Слайд 29
Описание слайда:
Proper ways

Слайд 30
Описание слайда:
Bubbling and Capturing

Слайд 31
Описание слайда:
Bubbling and Capturing

Слайд 32
Описание слайда:
Event object For every event in the browser instance of Event object will be created.

Слайд 33
Описание слайда:
Control of Default behavior Sometimes a default scenario of event processing includes some additional behavior: bubbling and capturing or displaying context menu.

Слайд 34
Описание слайда:
Sample

Слайд 35
Описание слайда:
Practice Task

Слайд 36
Описание слайда:
Advanced

Слайд 37
Описание слайда:

Слайд 38
Описание слайда:
Context Let's imagine two identical objects. They are created by Cat constructor:

Слайд 39
Описание слайда:
Context If we call method run() for both cats, we’ll take correct results:

Слайд 40
Описание слайда:
Context It works because we use the next form of access to attribute 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.

Слайд 41
Описание слайда:
Loss of context Be careful! There are situations when you can lose a context. For example:

Слайд 42
Описание слайда:

Слайд 43
Описание слайда:
Basic info Free space in browser sandbox is allocated for each variable in JavaScript. Sandbox is a special part of memory that will be managed by browser: JavaScript takes simplified and secure access to "memory“, browser translates JS commands and does all low-level work. As a result memory, PC and user data has protection from downloaded JavaScript malware.

Слайд 44
Описание слайда:
Scope The scope is a special JavaScript object which was created by browser in the sandbox and used for storing variables. Each function in JavaScript has its own personal scope. Scope is formed when a function is called and destroyed after the function finishes. This behavior helps to manage local variables mechanism. Object window is a top-level scope for all default and global variables.

Слайд 45
Описание слайда:
Scope

Слайд 46
Описание слайда:
Value-types and Reference-types Unfortunately some objects are too large for scope. For example string or function. There is simple division into value-types and reference-types for this reason. Value-types are stored in scope completely and for reference-types only reference to their location is put in scope. They themselves are located in place called "memory heap". String and all Objects are reference-types. Other data types are stored in scope.

Слайд 47
Описание слайда:
Memory cleaning The basic idea of memory cleaning: when function is finished, scope should be destroyed and as a result all local variables should be destroyed. This will work for value-types. As for reference-types: deleting the scope destroys only reference. The object in heap itself will be destroyed only when it becomes unreachable.

Слайд 48
Описание слайда:
Unreachable links An object is considered unreachable if it is not referenced from the client area of code. Garbage collector is responsible for the cleanup of unreachable objects. It's a special utility that will launch automatically if there isn’t enough space in the sandbox. If an object has at least one reference it is still reachable and will survive after memory cleaning.

Слайд 49
Описание слайда:
Unreachable links

Слайд 50
Описание слайда:

Слайд 51
Описание слайда:
Closure If scope is an object and it is not deleted it is still reachable, isn't it? Absolutely! This mechanism is called closure. If you save at least one reference to scope, all its content will survive after function finishing.

Слайд 52
Описание слайда:
Example

Слайд 53
Описание слайда:
Contacts


Скачать презентацию на тему Using objects in JavaScript. Accessing DOM in JavaScript можно ниже:

Похожие презентации