SlideShare a Scribd company logo
JavaScript
Get acquainted
TargetProcess
TP2 - Rich UI
TP2 - Rich UI
TP3 - high-performance UI
Goals
• JS popularization
• JS as engineering tool
JS Phenomena
Roadmap
• Embedded JS issues:
  - bad parts, good parts
  - imperative skin upon functional nature
  - misunderstanding of prototype inheritance
  - missing modules support
  - performance issues
Bad parts, good parts
Bad parts
•   global variables
•   eval
•   a++
•   math
•   with
•   new (Number | String | Boolean)
•   == vs ===
Good parts
•   ===      : type safe vs (==)
•   []       : new Array()
•   {}       : new Object()
•   a && b : if (a) b else a
•   a || b : if (a) a else b
•   closures (~ lambda curring)
Java(??!)Script
Java(??!)Script
•   ..one more LISP dialect..
•   Mocha
•   LiveScript
•   JavaScript
Programming paradigms
Functional nature
• functions are the 1-st class objects:
  - assign to variables
  - pass as an argument
  - return as a result
Inheritance?


Why?
OOP in JavaScript
• Объект_о-ориентированный
• Объектно-ориентированный
Prototype chain
Classical OOP simulation
• Classical OOP inheritance can be simulated:
  > I would recommend John Resig’s “Class”
  object
    http://ejohn.org/blog/simple-javascript-inheritance/
Classical OOP simulation
Class.extend({
    init: function(a, b) {
          // .ctor
          this._super(a, b);
    },
    method1: function() {
          // do something
    }
});
Inheritance examples
• Some examples
Inheritance examples
var F = function(n) {
  this.name = n;
}
var a = new F(“a”);
var b = new F(“b”);
Inheritance examples
var F = function(n) {
  this.name = n;
}
F.prototype = , root: “hello world!” -;
var a = new F(“a”);
var b = new F(“b”);
a.root // ???
b.root // ???
Inheritance examples
var F = function(n) {
   this.name = n;
}
F.prototype = , root: “hello world!” -;
var a = new F(“a”);
var b = new F(“b”);

a.root = “Prototype inheritance magic”;
b.root // ???
Inheritance examples
var F = function() {}

var a = new F();
a.constructor === F // ???
Inheritance examples
var F = function() {}
F.prototype = , root: “hello world!” -;
var a = new F();
a.constructor === F // ???
Dynamic inheritance
var F = function() {};
F.prototype = {
    count: 0,
    augment: function() {
            ++F.prototype.count;
            F.prototype.test = function() { alert(this.count) }
    }
};
var a = new F();
var b = new F();

a. augment();

a.test() // ???
b.test() // ???
Functions
• apply
• call
Modules
Modules simulation
• No modules. Global variables RULEZZZ!!11
Modules simulation
• No modules. Global variables RULEZZZ!!11



                 BAD!
Modules simulation
• No named modules. BUT functional context

       (function(global) { . . .})(window)
Modules simulation
• No named modules. BUT functional context

       (function(global) { . . .})(window)
Modules simulation
• No named modules. BUT functional context

       (function(global) { . . .})(window)

       var myJQueryVar = $.noConflict()
Modules simulation
• Namespacing as global variables chains
    newsite.common.utils
    newsite.common.services

  var newsite = newsite || {};
  newsite.common = newsite.common || {};
  newsite.common.utils = function() , … -;
Modules simulation
• Namespacing as global variables chains
     newsite.common.utils
     newsite.common.services
• $LAB
     .script(“newsite.core.js").wait()
     .script(“newsite.common.utils.js")
     .script(“newsite.common.services.js“)
     .wait(function() { /* ready */ })
Modules simulation


   RequireJS
  http://requirejs.org/
Modules simulation - RequireJS
<!DOCTYPE html>
<html>
  <head>
    <title>My Sample Project</title>
    <script
         src="path_to/require.js“
         data-main="entry_points/main">
    </script>
  </head>
  <body>
    <h1>My Sample Project</h1>
  </body>
</html>
entry_points/main.js
require(
   *“dir/module1“, “dir/module2“+,
   function(m1, m2) { /* to do: … */ }
);
dir/module1.js
define(
   *“dependency-on-some-other-modules”+,
   function () {
       return {
             color: "black",
             clear: function() ,…-
       };
   }
);
Performance
IE6?!
Performance - prologue
• It’s still possible to write slow JavaScript on
  the new fast JavaScript engines

• JavaScript performance directly affects user
  experience
High Performance JavaScript
Performance
• Loading & execution
• DOM scripting
Loading and execution
• Most browsers use a single UI thread for UI
  updates and JavaScript execution
• Appearance of a <script ..> tag cause page
  download and rendering to stop and wait for
  the script to complete before processing
• Even parallel script downloads block
  downloading other resources (images, CSS)
Loading and execution
• Put <script> tags as close to the bottom of the
  <body> as possible
• Load scripts in groups
  (100 kb faster than 4 x 25kb)
• Minify your scripts
• Optimize your stylesheets
Non-blocking loading
• <script defer> (IE 4+, FF 3.5+)
• Dynamic <script> elements
  – Parallel non-blocking loading
  – Put into <head> to prevent “operation aborted”
  – Remember of ordering (cross-browser variation)
• XMLHttpRequest injection
  – Inline <script> vs eval()
  – Downloading from CDNs impossible
RequireJS DO all the job!
DOM Scripting
• Live DOM collections
• Repaint and Reflow
• Handling DOM events
What is DOM?
• Document Object Model – language
  independent application interface (API) for
  working with XML and HTML documents
• Browsers keep DOM and JavaScript
  implementations independent of each other
Toll bridge
• Touch the DOM lightly
• Stay within ECMAScript as much as possible
HTML collections
• Expensive live collections
• Use local variables when accessing collection
  elements
Repaints and reflows
• DOM tree
• Render tree
Reflow process
When a DOM tree change affects element
geometry – browser recalculate geometry and
position of elements that could have been
affected by the change and reconstructs the
Render tree
Redraw process
Once the reflow is complete, the browser
redraws the affected parts of the screen
When does a reflow happen?
• Page renders initially
• Visible DOM elements are added or removed
• Elements change position
• Element change size
  (margin, padding, border, width, height)
• Content is changed (text or image with
  different size)
• Browser window is resized
Queuing and flushing reflows
• Browsers optimize reflow by queuing changes
  and performing them in batches
• Never request layout information while it’s
  being changed
Queuing and flushing reflows
•   offsetX
•   scrollX
•   clientX
•   getComputedStyle (currentStyle in IE)

* X – Top, Left, Width, Height
Minimizing repaints and reflows
• Combine multiple DOM and style changes into
  a batch and apply them once
Batching DOM changes
• Take the element off of the document flow
• Apply multiply changes
• Bring the element back to the document
Ways to modify the DOM off the
            document
• Hide, apply changes and show again
• Use document fragment to build subtree
  outside of the live DOM and then copy it to
  the document
• Copy the original element into an off-
  document node, modify the copy and replace
  original element when done
Take elements out of the flow for
             animation
1. Use absolute positioning
2. Animate the element
3. When the animation is done, restore the
   positioning

          JQuery DO this job for you!
Event delegation
• A lot of event handlers affects
  memory, performance and useless since user
  clicks 1 button of 100 for example
• Set event handler for container element and
  use event delegation
Performance essence
• http://jsperf.com/

• http://www.developer.nokia.com/Community
  /Wiki/JavaScript_Performance_Best_Practices
Patterns
• http://www.addyosmani.com/resources/esse
  ntialjsdesignpatterns/book/
How to move next
• Primary
• Advanced
• Meta-level
How to move next
• http://habrahabr.ru/post/117838/

More Related Content

PDF
Advanced JavaScript Development
PPT
Apache Velocity
PDF
COScheduler
PDF
Restful App Engine
PDF
Creating Dynamic Charts With JFreeChart
PPTX
PDF
ITB2017 - Slaying the ORM dragons with cborm
KEY
Lessons from a Dying CMS
Advanced JavaScript Development
Apache Velocity
COScheduler
Restful App Engine
Creating Dynamic Charts With JFreeChart
ITB2017 - Slaying the ORM dragons with cborm
Lessons from a Dying CMS

What's hot (20)

PPT
Practical JRuby
KEY
DSLs Internas e Ruby
PPTX
Introduction to Programming (well, kind of.)
KEY
QueryPath, Mash-ups, and Web Services
PDF
Apache Cayenne for WO Devs
PDF
Working with the django admin
PPT
Comprehensive Browser Automation Solution using Groovy, WebDriver & Obect Model
PPTX
Learning About JavaScript (…and its little buddy, JQuery!)
PDF
Fast Web Applications Development with Ruby on Rails on Oracle
PDF
Benchx: An XQuery benchmarking web application
PDF
Killing Shark-Riding Dinosaurs with ORM
PDF
Django Rest Framework and React and Redux, Oh My!
PDF
Not Just ORM: Powerful Hibernate ORM Features and Capabilities
PPTX
Object Oriented JavaScript - II
PDF
QueryPath: It's like PHP jQuery in Drupal!
KEY
The Inclusive Web: hands-on with HTML5 and jQuery
PDF
User defined-functions-cassandra-summit-eu-2014
PPTX
SenchaCon 2016: A Look Ahead: Survey Next-Gen Modern Browser APIs - Shikhir S...
PDF
Extending Oracle E-Business Suite with Ruby on Rails
PDF
XQuery in the Cloud
Practical JRuby
DSLs Internas e Ruby
Introduction to Programming (well, kind of.)
QueryPath, Mash-ups, and Web Services
Apache Cayenne for WO Devs
Working with the django admin
Comprehensive Browser Automation Solution using Groovy, WebDriver & Obect Model
Learning About JavaScript (…and its little buddy, JQuery!)
Fast Web Applications Development with Ruby on Rails on Oracle
Benchx: An XQuery benchmarking web application
Killing Shark-Riding Dinosaurs with ORM
Django Rest Framework and React and Redux, Oh My!
Not Just ORM: Powerful Hibernate ORM Features and Capabilities
Object Oriented JavaScript - II
QueryPath: It's like PHP jQuery in Drupal!
The Inclusive Web: hands-on with HTML5 and jQuery
User defined-functions-cassandra-summit-eu-2014
SenchaCon 2016: A Look Ahead: Survey Next-Gen Modern Browser APIs - Shikhir S...
Extending Oracle E-Business Suite with Ruby on Rails
XQuery in the Cloud
Ad

Viewers also liked (7)

PPTX
版本控制与常见的分支模型
PPTX
Mobile Disrupts the Cloud
PPTX
Trans cold express 6 page overview
PPT
PDF
Components now!
PDF
SEE Opportunity Mobile Application Development
PDF
Learn BEM: CSS Naming Convention
版本控制与常见的分支模型
Mobile Disrupts the Cloud
Trans cold express 6 page overview
Components now!
SEE Opportunity Mobile Application Development
Learn BEM: CSS Naming Convention
Ad

Similar to JS Essence (20)

PPTX
Javascript best practices
PPTX
01 Introduction - JavaScript Development
ODP
Javascript Update May 2013
PPTX
Javascript for web Programming creating and embedding with html
PDF
HTML5 for the Silverlight Guy
PPTX
JavaScript: the who, what, when, where, why, & how
PPTX
Introduction to java script, how to include java in HTML
PDF
Using RequireJS for Modular JavaScript Code
PPT
Introduction to Javascript
PDF
Building a JavaScript Library
KEY
JavaScript Neednt Hurt - JavaBin talk
PDF
Advanced guide to develop ajax applications using dojo
PDF
Kann JavaScript elegant sein?
PDF
50 common web developer interview questions [2020 updated] [www.full stack....
KEY
User Interface Development with jQuery
PPT
JavaScript Misunderstood
PPT
Html JavaScript and CSS
PPT
lecture 6 javascript event and event handling.ppt
PDF
Advancing JavaScript with Libraries (Yahoo Tech Talk)
Javascript best practices
01 Introduction - JavaScript Development
Javascript Update May 2013
Javascript for web Programming creating and embedding with html
HTML5 for the Silverlight Guy
JavaScript: the who, what, when, where, why, & how
Introduction to java script, how to include java in HTML
Using RequireJS for Modular JavaScript Code
Introduction to Javascript
Building a JavaScript Library
JavaScript Neednt Hurt - JavaBin talk
Advanced guide to develop ajax applications using dojo
Kann JavaScript elegant sein?
50 common web developer interview questions [2020 updated] [www.full stack....
User Interface Development with jQuery
JavaScript Misunderstood
Html JavaScript and CSS
lecture 6 javascript event and event handling.ppt
Advancing JavaScript with Libraries (Yahoo Tech Talk)

JS Essence