SlideShare una empresa de Scribd logo
1 de 31
JavaScript
Object-Oriented Programming
Remote Scripting
xqbase.com Development Team
webmaster@xqbase.com
Object-Oriented Programming
• Object
• Class
• Function
• Closure
• Example - Minesweeper
• Q & A
Remote Scripting
• XMLHttpRequest
• Cross-Origin Resource Sharing
• JSON with Padding
• Image Ping
• Polling, Comet, Streaming & WebSocket
• Q & A
Object-Oriented Programming
• Object
• Class
• Function
• Closure
• Example - Minesweeper
• Q & A
Variable Types
• Undefined var u;
• Boolean var b = false;
• Number var n = -0.12345;
• String var s = "foo";
• Function var f = function() {};
• Object var o = {}; // new Object();
• Null var on = null;
• Array var arr = []; // new Array();
Define an Object
• Initialize an Object
var square = {flag:0, mine:false};
• Add Members
square.adjacentSquares = new Array(8);
square.setFlag = function(flag) {
square.flag = flag;
};
square.hit = function() {
square.setFlag(square.mine ? -1 : 2);
};
• Usage
square.mine = true;
square.hit();
Object-Oriented Programming
• Object
• Class
• Function
• Closure
• Example - Minesweeper
• Q & A
Define a Class
• Constructor function Square(flag, mine) {
this.flag = flag;
this.mine = mine;
// this.setFlag = function(flag) { ... };
}
• Methods Square.prototype.setFlag = function(flag) {
this.flag = flag;
};
Square.prototype.hit = function() {
this.setFlag(this.mine ? -1 : 2);
};
• Usage var square = new Square(0, true);
square.hit();
Inheritance
• Constructor function HiddenSquare() {
Square.call(this, -2, false);
this.hidden = true;
}
• Methods HiddenSquare.prototype = new Square();
HiddenSquare.prototype.hit = function() {
alert("Unable to hit this square!");
}
• Usage var hiddenSquare = new HiddenSquare();
hiddenSquare.hit();
Object-Oriented Programming
• Object
• Class
• Function
• Closure
• Example - Minesweeper
• Q & A
Declaration vs. Expression
• Declaration
function hitSquare(square) {
square.hit();
}
• Expression
var hitSquare = function(square) {
square.hit();
};
Block Scope
(function() {
// block code here
})();
Object-Oriented Programming
• Object
• Class
• Function
• Closure
• Example - Minesweeper
• Q & A
Unable to Work 
for (var i = 0; i < 10; i ++) {
for (var j = 0; j < 10; j ++) {
var btn = document.createElement("input");
btn.type = "button";
btn.onclick = function() {
alert("(" + i + "," + j + ")");
};
document.body.appendChild(btn);
}
document.body.appendChild(document.createElement("br"));
}
Still Unable to Work 
for (var i = 0; i < 10; i ++) {
for (var j = 0; j < 10; j ++) {
var btn = document.createElement("input");
btn.type = "button";
var i_ = i;
var j_ = j;
btn.onclick = function() {
alert("(" + i_ + "," + j_ + ")");
};
document.body.appendChild(btn);
}
document.body.appendChild(document.createElement("br"));
}
Work with Closure 
for (var i = 0; i < 10; i ++) {
for (var j = 0; j < 10; j ++) {
var btn = document.createElement("input");
btn.type = "button";
btn.onclick = (function(i_, j_) {
return function() {
alert("(" + i_ + "," + j_ + ")");
};
})(i, j);
document.body.appendChild(btn);
}
document.body.appendChild(document.createElement("br"));
}
Object-Oriented Programming
• Object
• Class
• Function
• Closure
• Example - Minesweeper
• Q & A
Object-Oriented Programming
• Object
• Class
• Function
• Closure
• Example - Minesweeper
• Q & A
Remote Scripting
• XMLHttpRequest
• Cross-Origin Resource Sharing
• JSON with Padding
• Image Ping
• Polling, Comet, Streaming & WebSocket
• Q & A
XMLHttpRequest
function createXHR() {
if (typeof XMLHttpRequest == "undefined") {
return new ActiveXObject("MSXML2.XMLHttp");
}
return new XMLHttpRequest();
}
var xhr = createXHR();
xhr.open(...);
xhr.onload = function() {...};
xhr.onerror = function() {...};
xhr.send(...);
Remote Scripting
• XMLHttpRequest
• Cross-Origin Resource Sharing
• JSON with Padding
• Image Ping
• Polling, Comet, Streaming & WebSocket
• Q & A
Cross-Origin Resource Sharing
• Request
Origin: http://www.xqbase.com/
Access-Control-Request-Method: PUT
Access-Control-Request-Headers: X-Custom-Header
• Response
Access-Control-Allow-Origin: http://www.xqbase.com/
Access-Control-Allow-Credentials: true
Access-Control-Expose-Headers: FooBar
Access-Control-Allow-Methods: GET, POST, PUT
Access-Control-Allow-Headers: X-Custom-Header
Remote Scripting
• XMLHttpRequest
• Cross-Origin Resource Sharing
• JSON with Padding
• Image Ping
• Polling, Comet, Streaming & WebSocket
• Q & A
JSON with Padding
function locationCallback(param) {
alert("You're at IP address " + param.ip +
", which is in " + param.city + ", " +
param.region_name);
}
var script = document.createElement("script");
script.src = "http://freegeoip.net/json/?callback=" +
"locationCallback&random=" + Math.random();
document.body.insertBefore(script,
document.body.firstChild);
Remote Scripting
• XMLHttpRequest
• Cross-Origin Resource Sharing
• JSON with Padding
• Image Ping
• Polling, Comet, Streaming & WebSocket
• Q & A
Image Ping
var img = new Image();
img.onload = function() {...};
img.onerror = function() {...};
img.src = "http://www.xqbase.com/ping";
Remote Scripting
• XMLHttpRequest
• Cross-Origin Resource Sharing
• JSON with Padding
• Image Ping
• Polling, Comet, Streaming & WebSocket
• Q & A
Polling Comet
Polling, Comet, Streaming and WebSocket
Streaming WebSocket
Polling, Comet, Streaming and WebSocket
Polling Comet Streaming WebSocket
Timelineness Bad Good Good Good
Performance Bad Poor Good Excellent
Browser Recv.
Frequently
HTTP Req.
Wait for
HTTP Resp.
In Channel In Channel
Browser Send
Need One
HTTP Req.
Need One
HTTP Req.
Need One
HTTP Req.
In Channel
Browser Tech
XmlHttpReq.
or JSONP
XmlHttpReq.
or JSONP
XmlHttpReq.
readyState ==
3 (LOADING)
WebSocket
Server Tech
Traditional
Web Server
Async
Async +
Streaming
WebSocket
Remote Scripting
• XMLHttpRequest
• Cross-Origin Resource Sharing
• JSON with Padding
• Image Ping
• Polling, Comet, Streaming & WebSocket
• Q & A
Thanks
xqbase.com Development Team
webmaster@xqbase.com
JavaScript
• Object-Oriented Programming
• Remote Scripting

Más contenido relacionado

La actualidad más candente

Teaching Native Qt to Talk Web
Teaching Native Qt to Talk WebTeaching Native Qt to Talk Web
Teaching Native Qt to Talk WebAlan Uthoff
 
CS442 - Rogue: A Scala DSL for MongoDB
CS442 - Rogue: A Scala DSL for MongoDBCS442 - Rogue: A Scala DSL for MongoDB
CS442 - Rogue: A Scala DSL for MongoDBjorgeortiz85
 
Leveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHPLeveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHPJeremy Kendall
 
Leveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHPLeveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHPJeremy Kendall
 
Php 102: Out with the Bad, In with the Good
Php 102: Out with the Bad, In with the GoodPhp 102: Out with the Bad, In with the Good
Php 102: Out with the Bad, In with the GoodJeremy Kendall
 
PDF.JS at SwissJeese 2012
PDF.JS at SwissJeese 2012PDF.JS at SwissJeese 2012
PDF.JS at SwissJeese 2012Julian Viereck
 
FIFA fails, Guy Kawasaki and real estate in SF - find out about all three by ...
FIFA fails, Guy Kawasaki and real estate in SF - find out about all three by ...FIFA fails, Guy Kawasaki and real estate in SF - find out about all three by ...
FIFA fails, Guy Kawasaki and real estate in SF - find out about all three by ...Elżbieta Bednarek
 
Jsquery - the jsonb query language with GIN indexing support
Jsquery - the jsonb query language with GIN indexing supportJsquery - the jsonb query language with GIN indexing support
Jsquery - the jsonb query language with GIN indexing supportAlexander Korotkov
 
A Short Introduction To jQuery
A Short Introduction To jQueryA Short Introduction To jQuery
A Short Introduction To jQuerySudar Muthu
 
HTML5 and CSS3 Refresher
HTML5 and CSS3 RefresherHTML5 and CSS3 Refresher
HTML5 and CSS3 RefresherIvano Malavolta
 
Learn JavaScript by modeling Rubik Cube
Learn JavaScript by modeling Rubik CubeLearn JavaScript by modeling Rubik Cube
Learn JavaScript by modeling Rubik CubeManoj Kumar
 
Scala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereldScala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereldWerner Hofstra
 
Out ofmemoryerror what is the cost of java objects
Out ofmemoryerror  what is the cost of java objectsOut ofmemoryerror  what is the cost of java objects
Out ofmemoryerror what is the cost of java objectsJean-Philippe BEMPEL
 
Ajax tutorial
Ajax tutorialAjax tutorial
Ajax tutorialKat Roque
 

La actualidad más candente (20)

Teaching Native Qt to Talk Web
Teaching Native Qt to Talk WebTeaching Native Qt to Talk Web
Teaching Native Qt to Talk Web
 
CS442 - Rogue: A Scala DSL for MongoDB
CS442 - Rogue: A Scala DSL for MongoDBCS442 - Rogue: A Scala DSL for MongoDB
CS442 - Rogue: A Scala DSL for MongoDB
 
Leveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHPLeveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHP
 
Leveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHPLeveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHP
 
Web2.0 with jQuery in English
Web2.0 with jQuery in EnglishWeb2.0 with jQuery in English
Web2.0 with jQuery in English
 
Php 102: Out with the Bad, In with the Good
Php 102: Out with the Bad, In with the GoodPhp 102: Out with the Bad, In with the Good
Php 102: Out with the Bad, In with the Good
 
D3.js and SVG
D3.js and SVGD3.js and SVG
D3.js and SVG
 
PDF.JS at SwissJeese 2012
PDF.JS at SwissJeese 2012PDF.JS at SwissJeese 2012
PDF.JS at SwissJeese 2012
 
FIFA fails, Guy Kawasaki and real estate in SF - find out about all three by ...
FIFA fails, Guy Kawasaki and real estate in SF - find out about all three by ...FIFA fails, Guy Kawasaki and real estate in SF - find out about all three by ...
FIFA fails, Guy Kawasaki and real estate in SF - find out about all three by ...
 
Oh, that ubiquitous JSON !
Oh, that ubiquitous JSON !Oh, that ubiquitous JSON !
Oh, that ubiquitous JSON !
 
Jsquery - the jsonb query language with GIN indexing support
Jsquery - the jsonb query language with GIN indexing supportJsquery - the jsonb query language with GIN indexing support
Jsquery - the jsonb query language with GIN indexing support
 
ECMAScript 5: Новое в JavaScript
ECMAScript 5: Новое в JavaScriptECMAScript 5: Новое в JavaScript
ECMAScript 5: Новое в JavaScript
 
A Short Introduction To jQuery
A Short Introduction To jQueryA Short Introduction To jQuery
A Short Introduction To jQuery
 
HTML5 and CSS3 Refresher
HTML5 and CSS3 RefresherHTML5 and CSS3 Refresher
HTML5 and CSS3 Refresher
 
Learn JavaScript by modeling Rubik Cube
Learn JavaScript by modeling Rubik CubeLearn JavaScript by modeling Rubik Cube
Learn JavaScript by modeling Rubik Cube
 
JavaScript Obfuscation
JavaScript ObfuscationJavaScript Obfuscation
JavaScript Obfuscation
 
JavaScript Patterns
JavaScript PatternsJavaScript Patterns
JavaScript Patterns
 
Scala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereldScala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereld
 
Out ofmemoryerror what is the cost of java objects
Out ofmemoryerror  what is the cost of java objectsOut ofmemoryerror  what is the cost of java objects
Out ofmemoryerror what is the cost of java objects
 
Ajax tutorial
Ajax tutorialAjax tutorial
Ajax tutorial
 

Destacado

"JS: the right way" by Mykyta Semenistyi
"JS: the right way" by Mykyta Semenistyi"JS: the right way" by Mykyta Semenistyi
"JS: the right way" by Mykyta SemenistyiBinary Studio
 
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...Doug Jones
 
Ensalada tricolor
Ensalada tricolorEnsalada tricolor
Ensalada tricolorkarmela111
 
Informe maibely salcedo
Informe maibely salcedoInforme maibely salcedo
Informe maibely salcedoMaibelySalcedo
 
Redis in Practice: Scenarios, Performance and Practice with PHP
Redis in Practice: Scenarios, Performance and Practice with PHPRedis in Practice: Scenarios, Performance and Practice with PHP
Redis in Practice: Scenarios, Performance and Practice with PHPChen Huang
 
Leadership 2.0: leading in the digital age
Leadership 2.0: leading in the digital ageLeadership 2.0: leading in the digital age
Leadership 2.0: leading in the digital ageStefan F. Dieffenbacher
 
Media institutions powerpoint
Media institutions powerpointMedia institutions powerpoint
Media institutions powerpointDenton Snowden
 

Destacado (13)

"JS: the right way" by Mykyta Semenistyi
"JS: the right way" by Mykyta Semenistyi"JS: the right way" by Mykyta Semenistyi
"JS: the right way" by Mykyta Semenistyi
 
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
 
Ead x eol
Ead x eolEad x eol
Ead x eol
 
Ensalada tricolor
Ensalada tricolorEnsalada tricolor
Ensalada tricolor
 
Unit 67 task 2
Unit 67   task 2Unit 67   task 2
Unit 67 task 2
 
Vertebrados
VertebradosVertebrados
Vertebrados
 
Contents pages
Contents pagesContents pages
Contents pages
 
Style sheet
Style sheetStyle sheet
Style sheet
 
Big 5
Big 5Big 5
Big 5
 
Informe maibely salcedo
Informe maibely salcedoInforme maibely salcedo
Informe maibely salcedo
 
Redis in Practice: Scenarios, Performance and Practice with PHP
Redis in Practice: Scenarios, Performance and Practice with PHPRedis in Practice: Scenarios, Performance and Practice with PHP
Redis in Practice: Scenarios, Performance and Practice with PHP
 
Leadership 2.0: leading in the digital age
Leadership 2.0: leading in the digital ageLeadership 2.0: leading in the digital age
Leadership 2.0: leading in the digital age
 
Media institutions powerpoint
Media institutions powerpointMedia institutions powerpoint
Media institutions powerpoint
 

Similar a JavaScript - Object-Oriented Programming & Remote Scripting

Javascript Everywhere
Javascript EverywhereJavascript Everywhere
Javascript EverywherePascal Rettig
 
How I Learned to Stop Worrying and Love jQuery (Jan 2013)
How I Learned to Stop Worrying and Love jQuery (Jan 2013)How I Learned to Stop Worrying and Love jQuery (Jan 2013)
How I Learned to Stop Worrying and Love jQuery (Jan 2013)David Giard
 
Cross Domain Web
Mashups with JQuery and Google App Engine
Cross Domain Web
Mashups with JQuery and Google App EngineCross Domain Web
Mashups with JQuery and Google App Engine
Cross Domain Web
Mashups with JQuery and Google App EngineAndy McKay
 
Painless Persistence in a Disconnected World
Painless Persistence in a Disconnected WorldPainless Persistence in a Disconnected World
Painless Persistence in a Disconnected WorldChristian Melchior
 
JSLT: JSON querying and transformation
JSLT: JSON querying and transformationJSLT: JSON querying and transformation
JSLT: JSON querying and transformationLars Marius Garshol
 
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)Doris Chen
 
Awesomeness of JavaScript…almost
Awesomeness of JavaScript…almostAwesomeness of JavaScript…almost
Awesomeness of JavaScript…almostQuinton Sheppard
 
Testing swagger contracts without contract based testing
Testing swagger contracts without contract based testingTesting swagger contracts without contract based testing
Testing swagger contracts without contract based testingАлексей Стягайло
 
Ajax Under The Hood
Ajax Under The HoodAjax Under The Hood
Ajax Under The HoodWO Community
 
Khan Academy Computer Science
Khan Academy Computer ScienceKhan Academy Computer Science
Khan Academy Computer Sciencejeresig
 
Javascript Libraries
Javascript LibrariesJavascript Libraries
Javascript Librarieselliando dias
 
Paperjs presentation
Paperjs presentationPaperjs presentation
Paperjs presentationsharp-blade
 
Azure for SharePoint Developers - Workshop - Part 2: Azure Functions
Azure for SharePoint Developers - Workshop - Part 2: Azure FunctionsAzure for SharePoint Developers - Workshop - Part 2: Azure Functions
Azure for SharePoint Developers - Workshop - Part 2: Azure FunctionsBob German
 
jQuery Data Manipulate API - A source code dissecting journey
jQuery Data Manipulate API - A source code dissecting journeyjQuery Data Manipulate API - A source code dissecting journey
jQuery Data Manipulate API - A source code dissecting journeyHuiyi Yan
 
CouchDB on Android
CouchDB on AndroidCouchDB on Android
CouchDB on AndroidSven Haiges
 

Similar a JavaScript - Object-Oriented Programming & Remote Scripting (20)

bcgr3-jquery
bcgr3-jquerybcgr3-jquery
bcgr3-jquery
 
bcgr3-jquery
bcgr3-jquerybcgr3-jquery
bcgr3-jquery
 
Javascript Everywhere
Javascript EverywhereJavascript Everywhere
Javascript Everywhere
 
huhu
huhuhuhu
huhu
 
How I Learned to Stop Worrying and Love jQuery (Jan 2013)
How I Learned to Stop Worrying and Love jQuery (Jan 2013)How I Learned to Stop Worrying and Love jQuery (Jan 2013)
How I Learned to Stop Worrying and Love jQuery (Jan 2013)
 
Cross Domain Web
Mashups with JQuery and Google App Engine
Cross Domain Web
Mashups with JQuery and Google App EngineCross Domain Web
Mashups with JQuery and Google App Engine
Cross Domain Web
Mashups with JQuery and Google App Engine
 
Painless Persistence in a Disconnected World
Painless Persistence in a Disconnected WorldPainless Persistence in a Disconnected World
Painless Persistence in a Disconnected World
 
JSLT: JSON querying and transformation
JSLT: JSON querying and transformationJSLT: JSON querying and transformation
JSLT: JSON querying and transformation
 
Js training
Js trainingJs training
Js training
 
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
 
Awesomeness of JavaScript…almost
Awesomeness of JavaScript…almostAwesomeness of JavaScript…almost
Awesomeness of JavaScript…almost
 
Testing swagger contracts without contract based testing
Testing swagger contracts without contract based testingTesting swagger contracts without contract based testing
Testing swagger contracts without contract based testing
 
Ajax Under The Hood
Ajax Under The HoodAjax Under The Hood
Ajax Under The Hood
 
Khan Academy Computer Science
Khan Academy Computer ScienceKhan Academy Computer Science
Khan Academy Computer Science
 
Javascript Libraries
Javascript LibrariesJavascript Libraries
Javascript Libraries
 
Paperjs presentation
Paperjs presentationPaperjs presentation
Paperjs presentation
 
Azure for SharePoint Developers - Workshop - Part 2: Azure Functions
Azure for SharePoint Developers - Workshop - Part 2: Azure FunctionsAzure for SharePoint Developers - Workshop - Part 2: Azure Functions
Azure for SharePoint Developers - Workshop - Part 2: Azure Functions
 
jQuery Data Manipulate API - A source code dissecting journey
jQuery Data Manipulate API - A source code dissecting journeyjQuery Data Manipulate API - A source code dissecting journey
jQuery Data Manipulate API - A source code dissecting journey
 
CouchDB on Android
CouchDB on AndroidCouchDB on Android
CouchDB on Android
 
Performance patterns
Performance patternsPerformance patterns
Performance patterns
 

Último

『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书rnrncn29
 
SCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is prediSCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is predieusebiomeyer
 
Unidad 4 – Redes de ordenadores (en inglés).pptx
Unidad 4 – Redes de ordenadores (en inglés).pptxUnidad 4 – Redes de ordenadores (en inglés).pptx
Unidad 4 – Redes de ordenadores (en inglés).pptxmibuzondetrabajo
 
Cybersecurity Threats and Cybersecurity Best Practices
Cybersecurity Threats and Cybersecurity Best PracticesCybersecurity Threats and Cybersecurity Best Practices
Cybersecurity Threats and Cybersecurity Best PracticesLumiverse Solutions Pvt Ltd
 
TRENDS Enabling and inhibiting dimensions.pptx
TRENDS Enabling and inhibiting dimensions.pptxTRENDS Enabling and inhibiting dimensions.pptx
TRENDS Enabling and inhibiting dimensions.pptxAndrieCagasanAkio
 
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书rnrncn29
 
Company Snapshot Theme for Business by Slidesgo.pptx
Company Snapshot Theme for Business by Slidesgo.pptxCompany Snapshot Theme for Business by Slidesgo.pptx
Company Snapshot Theme for Business by Slidesgo.pptxMario
 
IP addressing and IPv6, presented by Paul Wilson at IETF 119
IP addressing and IPv6, presented by Paul Wilson at IETF 119IP addressing and IPv6, presented by Paul Wilson at IETF 119
IP addressing and IPv6, presented by Paul Wilson at IETF 119APNIC
 
ETHICAL HACKING dddddddddddddddfnandni.pptx
ETHICAL HACKING dddddddddddddddfnandni.pptxETHICAL HACKING dddddddddddddddfnandni.pptx
ETHICAL HACKING dddddddddddddddfnandni.pptxNIMMANAGANTI RAMAKRISHNA
 

Último (9)

『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
 
SCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is prediSCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is predi
 
Unidad 4 – Redes de ordenadores (en inglés).pptx
Unidad 4 – Redes de ordenadores (en inglés).pptxUnidad 4 – Redes de ordenadores (en inglés).pptx
Unidad 4 – Redes de ordenadores (en inglés).pptx
 
Cybersecurity Threats and Cybersecurity Best Practices
Cybersecurity Threats and Cybersecurity Best PracticesCybersecurity Threats and Cybersecurity Best Practices
Cybersecurity Threats and Cybersecurity Best Practices
 
TRENDS Enabling and inhibiting dimensions.pptx
TRENDS Enabling and inhibiting dimensions.pptxTRENDS Enabling and inhibiting dimensions.pptx
TRENDS Enabling and inhibiting dimensions.pptx
 
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
 
Company Snapshot Theme for Business by Slidesgo.pptx
Company Snapshot Theme for Business by Slidesgo.pptxCompany Snapshot Theme for Business by Slidesgo.pptx
Company Snapshot Theme for Business by Slidesgo.pptx
 
IP addressing and IPv6, presented by Paul Wilson at IETF 119
IP addressing and IPv6, presented by Paul Wilson at IETF 119IP addressing and IPv6, presented by Paul Wilson at IETF 119
IP addressing and IPv6, presented by Paul Wilson at IETF 119
 
ETHICAL HACKING dddddddddddddddfnandni.pptx
ETHICAL HACKING dddddddddddddddfnandni.pptxETHICAL HACKING dddddddddddddddfnandni.pptx
ETHICAL HACKING dddddddddddddddfnandni.pptx
 

JavaScript - Object-Oriented Programming & Remote Scripting

  • 2. Object-Oriented Programming • Object • Class • Function • Closure • Example - Minesweeper • Q & A
  • 3. Remote Scripting • XMLHttpRequest • Cross-Origin Resource Sharing • JSON with Padding • Image Ping • Polling, Comet, Streaming & WebSocket • Q & A
  • 4. Object-Oriented Programming • Object • Class • Function • Closure • Example - Minesweeper • Q & A
  • 5. Variable Types • Undefined var u; • Boolean var b = false; • Number var n = -0.12345; • String var s = "foo"; • Function var f = function() {}; • Object var o = {}; // new Object(); • Null var on = null; • Array var arr = []; // new Array();
  • 6. Define an Object • Initialize an Object var square = {flag:0, mine:false}; • Add Members square.adjacentSquares = new Array(8); square.setFlag = function(flag) { square.flag = flag; }; square.hit = function() { square.setFlag(square.mine ? -1 : 2); }; • Usage square.mine = true; square.hit();
  • 7. Object-Oriented Programming • Object • Class • Function • Closure • Example - Minesweeper • Q & A
  • 8. Define a Class • Constructor function Square(flag, mine) { this.flag = flag; this.mine = mine; // this.setFlag = function(flag) { ... }; } • Methods Square.prototype.setFlag = function(flag) { this.flag = flag; }; Square.prototype.hit = function() { this.setFlag(this.mine ? -1 : 2); }; • Usage var square = new Square(0, true); square.hit();
  • 9. Inheritance • Constructor function HiddenSquare() { Square.call(this, -2, false); this.hidden = true; } • Methods HiddenSquare.prototype = new Square(); HiddenSquare.prototype.hit = function() { alert("Unable to hit this square!"); } • Usage var hiddenSquare = new HiddenSquare(); hiddenSquare.hit();
  • 10. Object-Oriented Programming • Object • Class • Function • Closure • Example - Minesweeper • Q & A
  • 11. Declaration vs. Expression • Declaration function hitSquare(square) { square.hit(); } • Expression var hitSquare = function(square) { square.hit(); };
  • 12. Block Scope (function() { // block code here })();
  • 13. Object-Oriented Programming • Object • Class • Function • Closure • Example - Minesweeper • Q & A
  • 14. Unable to Work  for (var i = 0; i < 10; i ++) { for (var j = 0; j < 10; j ++) { var btn = document.createElement("input"); btn.type = "button"; btn.onclick = function() { alert("(" + i + "," + j + ")"); }; document.body.appendChild(btn); } document.body.appendChild(document.createElement("br")); }
  • 15. Still Unable to Work  for (var i = 0; i < 10; i ++) { for (var j = 0; j < 10; j ++) { var btn = document.createElement("input"); btn.type = "button"; var i_ = i; var j_ = j; btn.onclick = function() { alert("(" + i_ + "," + j_ + ")"); }; document.body.appendChild(btn); } document.body.appendChild(document.createElement("br")); }
  • 16. Work with Closure  for (var i = 0; i < 10; i ++) { for (var j = 0; j < 10; j ++) { var btn = document.createElement("input"); btn.type = "button"; btn.onclick = (function(i_, j_) { return function() { alert("(" + i_ + "," + j_ + ")"); }; })(i, j); document.body.appendChild(btn); } document.body.appendChild(document.createElement("br")); }
  • 17. Object-Oriented Programming • Object • Class • Function • Closure • Example - Minesweeper • Q & A
  • 18. Object-Oriented Programming • Object • Class • Function • Closure • Example - Minesweeper • Q & A
  • 19. Remote Scripting • XMLHttpRequest • Cross-Origin Resource Sharing • JSON with Padding • Image Ping • Polling, Comet, Streaming & WebSocket • Q & A
  • 20. XMLHttpRequest function createXHR() { if (typeof XMLHttpRequest == "undefined") { return new ActiveXObject("MSXML2.XMLHttp"); } return new XMLHttpRequest(); } var xhr = createXHR(); xhr.open(...); xhr.onload = function() {...}; xhr.onerror = function() {...}; xhr.send(...);
  • 21. Remote Scripting • XMLHttpRequest • Cross-Origin Resource Sharing • JSON with Padding • Image Ping • Polling, Comet, Streaming & WebSocket • Q & A
  • 22. Cross-Origin Resource Sharing • Request Origin: http://www.xqbase.com/ Access-Control-Request-Method: PUT Access-Control-Request-Headers: X-Custom-Header • Response Access-Control-Allow-Origin: http://www.xqbase.com/ Access-Control-Allow-Credentials: true Access-Control-Expose-Headers: FooBar Access-Control-Allow-Methods: GET, POST, PUT Access-Control-Allow-Headers: X-Custom-Header
  • 23. Remote Scripting • XMLHttpRequest • Cross-Origin Resource Sharing • JSON with Padding • Image Ping • Polling, Comet, Streaming & WebSocket • Q & A
  • 24. JSON with Padding function locationCallback(param) { alert("You're at IP address " + param.ip + ", which is in " + param.city + ", " + param.region_name); } var script = document.createElement("script"); script.src = "http://freegeoip.net/json/?callback=" + "locationCallback&random=" + Math.random(); document.body.insertBefore(script, document.body.firstChild);
  • 25. Remote Scripting • XMLHttpRequest • Cross-Origin Resource Sharing • JSON with Padding • Image Ping • Polling, Comet, Streaming & WebSocket • Q & A
  • 26. Image Ping var img = new Image(); img.onload = function() {...}; img.onerror = function() {...}; img.src = "http://www.xqbase.com/ping";
  • 27. Remote Scripting • XMLHttpRequest • Cross-Origin Resource Sharing • JSON with Padding • Image Ping • Polling, Comet, Streaming & WebSocket • Q & A
  • 28. Polling Comet Polling, Comet, Streaming and WebSocket Streaming WebSocket
  • 29. Polling, Comet, Streaming and WebSocket Polling Comet Streaming WebSocket Timelineness Bad Good Good Good Performance Bad Poor Good Excellent Browser Recv. Frequently HTTP Req. Wait for HTTP Resp. In Channel In Channel Browser Send Need One HTTP Req. Need One HTTP Req. Need One HTTP Req. In Channel Browser Tech XmlHttpReq. or JSONP XmlHttpReq. or JSONP XmlHttpReq. readyState == 3 (LOADING) WebSocket Server Tech Traditional Web Server Async Async + Streaming WebSocket
  • 30. Remote Scripting • XMLHttpRequest • Cross-Origin Resource Sharing • JSON with Padding • Image Ping • Polling, Comet, Streaming & WebSocket • Q & A
  • 31. Thanks xqbase.com Development Team webmaster@xqbase.com JavaScript • Object-Oriented Programming • Remote Scripting