SlideShare a Scribd company logo
OO JS for AS3 Devs Object-Oriented JavaScript for ActionScript 3 Developers
Pirates vs Ninjas Pirate Ninja
JS vs AS3 Object Number String Boolean Object Object Array (Object)  Date Function Function Prototype <Script src=&quot;&quot;/> Function Object Number | int | unit String Boolean Array Dictionary Array Date Function Class Prototype Import Getter / Setter
No Strong Types in JS var myVar1; var myVar1 = 10; var myVar1, myVar2, myVar3; var myVar = []; myVar.push(&quot;foo&quot;); const EVENT_NAME = &quot;name&quot; var myVar:Object public myVar public function myFunct():String function myFunct():String package {}, class {}  extends, implements, final, internal, public, private GOOD PUPPY  BAD PUPPY 
'Classes' in JS     Everything is an Object or Function Functions are pushed to the limits A JS Class is just a Function Function 'scope' rules are used for to create public, private spaces Inheritance is wacky ( will get to this later ) Prototype is wacky ( will get to this later )  
function myClass() {      this.add = function(a, b)          {          return a + b;      }; } var c = new myClass(); c.add(1, 1); TEST IN FIREBUG
Getters / Setters function Puppy() {      var _evil = 1000;      this. getEvil  = function()      {          return _evil;      };      this. setEvil  = function(value)      {          _evil  = value;      } } var evilPuppy = new Puppy(); evilPuppy. getEvil() ;
Self Executing Functions function Puppy() {      var _evil = 999;      this.getEvil = function()      {          return _evil;      };      (function init(value)      {          _evil = v;      })(45) } var evilPuppy = new Puppy(); evilPuppy.getEvil();
Scope is a Pain! function level_1() {      var good = 20;      this.evil = 99;      var timeout = setTimeout(function()          {              console.log(good);              console.log(evil);              console.log(this.evil);          }, 500); } var x = new level_1();
Scope is a Pain - Part 2 function level_1() {      var good = 20;      this.evil = 99;      var scope = this;      var timeout = setTimeout(function()          {              console.log(good);              console.log(scope.evil);          }, 500); } var x = new level_1();
Inheritance Tons of ways to do Inheritance  I will only cover one way No &quot;extends&quot; in JS Completely different way of thinking about it Special property on Object named Prototype
function Animal() {      Animal.prototype.evil = 1000;      Animal.prototype.getColor = function()      {          return &quot;Blood Red&quot;;      } } function Puppy()  { } Puppy.prototype = Animal.prototype; var p = new Puppy(); p.getColor();
function Animal() {      Animal.prototype.evil = 1000;      Animal.prototype.getColor = function()      {          return &quot;Blood Red&quot;;      } } function Puppy()  { } new Animal(); //(GOTCHA!) Puppy.prototype = Animal.prototype; var p = new Puppy(); p.getColor();
function Animal(){      Animal.prototype.color = &quot;Blood Red&quot;;      Animal.prototype.getColor = function()      {          return &quot;Evil &quot; + this.color;      } }; var a = new Animal(); console.log(&quot;Animal Color: &quot; + a.getColor()); function Puppy() {      this.color = &quot;Blue&quot;; } Puppy.prototype = Animal.prototype; var p = new Puppy(); console.log(&quot;Puppy Color: &quot; + p.getColor());
HTML5 <canvas> tag  <body> <canvas id=&quot;gameCanvas&quot; width=&quot;600&quot; height=&quot;600&quot;></canvas> </body>
Get Pointer to <canvas> from JS // javaScript var canvas = document.getElementById('gameCanvas'); var context = canvas.getContext('2d');
Draw on <canvas> with JS // javaScript context.beginPath(); context.moveTo(0,0); context.lineTo(0,100); context.lineTo(100,100); context.lineTo(100,0); context.closePath(); context.strokeStyle = &quot;#000000&quot;; context.fill(); context.fillStyle = &quot;#CCCCCC&quot;; context.fill();
&quot;Animation&quot; (in quotes) var intervalId = setInterval(drawCanvas, 1000 / 30); function drawCanvas(){      // clear everything      context.clearRect(0,0, myWidth, myHeight);      // redraw everything      // Yes, you better know exactly       // where every pixel is at all times so       // you can redraw it at 30 FPS      // :( }
&quot;Animation&quot; (in quotes) var intervalId = setInterval(smartDrawCanvas, 1000 / 30); function smartDrawCanvas() {      // Clear only part of the canvas      context.clearRect(25, 25, 50, 50);      // redraw SOME of the canvas      // TODO .... }
Mouse Events on <canvas> Cannot listen to events on any children on the <canvas> Think of <canvas> as AS3 Bitmap You can only listen on the main <canvas> object You need to know where every 'interactive' object is the <canvas> at all times. For clicks you need to calculate what was under the pixel that was clicked on Observer pattern helps with this work
Gotchas **JS** (for each works in FF, but not Chrome) for (var x in myArray) {      var obj = myArray[x];      obj.selected = false; } **AS3** for each (var x:Object in myArray) {      x.selected = false; }
Gotchas var timeout = setTimeout(function()      {                  var x = passThruVar1;          // x is undefined!!!      }, 500,  passThruVar1 ,  passThruVar2 );

More Related Content

PDF
Python Coroutines, Present and Future
PPTX
ES6 Overview
PDF
Don't do this
KEY
Python Yield
PDF
PDF
Imugi: Compiler made with Python
PDF
Apache PIG - User Defined Functions
PPT
JSConf: All You Can Leet
Python Coroutines, Present and Future
ES6 Overview
Don't do this
Python Yield
Imugi: Compiler made with Python
Apache PIG - User Defined Functions
JSConf: All You Can Leet

What's hot (20)

ZIP
Intro to Pig UDF
PDF
Advanced python
PDF
Docopt
PDF
node ffi
KEY
Gevent what's the point
PDF
EcmaScript 6 - The future is here
KEY
XpUg Coding Dojo: KataYahtzee in Ocp way
PPT
JavaScript Functions
KEY
Agile Iphone Development
PDF
Stupid Awesome Python Tricks
PPTX
Stop Programming in JavaScript By Luck
PPTX
Javascript Function
PDF
The Evolution of Async-Programming (SD 2.0, JavaScript)
PDF
A swift introduction to Swift
PPT
Lecture05
PDF
The Evolution of Async-Programming on .NET Platform (TUP, Full)
PPTX
A Few Interesting Things in Apple's Swift Programming Language
KEY
PPT
PPT
C++totural file
Intro to Pig UDF
Advanced python
Docopt
node ffi
Gevent what's the point
EcmaScript 6 - The future is here
XpUg Coding Dojo: KataYahtzee in Ocp way
JavaScript Functions
Agile Iphone Development
Stupid Awesome Python Tricks
Stop Programming in JavaScript By Luck
Javascript Function
The Evolution of Async-Programming (SD 2.0, JavaScript)
A swift introduction to Swift
Lecture05
The Evolution of Async-Programming on .NET Platform (TUP, Full)
A Few Interesting Things in Apple's Swift Programming Language
C++totural file
Ad

Similar to OO JS for AS3 Devs (20)

PDF
Js objects
PPTX
Object Oriented JavaScript
PPTX
Intro to Canva
PPTX
Introduction to Canvas - Toronto HTML5 User Group
PDF
Introduction to Canvas - Toronto HTML5 User Group
PDF
Joose - JavaScript Meta Object System
KEY
The Canvas API for Rubyists
PPTX
OO in JavaScript
PPT
Beginning Object-Oriented JavaScript
PDF
Javascript Styles and some tips
PPTX
JavaScript, Beyond the Curly Braces
PDF
Say It With Javascript
PDF
Intro to Advanced JavaScript
PPT
HTML5 Canvas
PDF
HTML5 Canvas - The Future of Graphics on the Web
PDF
JavaScript and UI Architecture Best Practices
PDF
Tamarin And Ecmascript 4
PPT
JavaScript Basics
PPT
Advanced JavaScript
Js objects
Object Oriented JavaScript
Intro to Canva
Introduction to Canvas - Toronto HTML5 User Group
Introduction to Canvas - Toronto HTML5 User Group
Joose - JavaScript Meta Object System
The Canvas API for Rubyists
OO in JavaScript
Beginning Object-Oriented JavaScript
Javascript Styles and some tips
JavaScript, Beyond the Curly Braces
Say It With Javascript
Intro to Advanced JavaScript
HTML5 Canvas
HTML5 Canvas - The Future of Graphics on the Web
JavaScript and UI Architecture Best Practices
Tamarin And Ecmascript 4
JavaScript Basics
Advanced JavaScript
Ad

More from Jason Hanson (6)

PPTX
HelloGit
PPT
Reinvent yourself - How to become a native iOS developer in nine steps
KEY
Mobile Flex - Flash Camp St. Louis
KEY
Maximizing Code Reuse Across Screens
KEY
Deep Dive into Flex Mobile Item Renderers
PDF
Burrito and Hero
HelloGit
Reinvent yourself - How to become a native iOS developer in nine steps
Mobile Flex - Flash Camp St. Louis
Maximizing Code Reuse Across Screens
Deep Dive into Flex Mobile Item Renderers
Burrito and Hero

Recently uploaded (20)

PDF
Empathic Computing: Creating Shared Understanding
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
KodekX | Application Modernization Development
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Encapsulation theory and applications.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Unlocking AI with Model Context Protocol (MCP)
Empathic Computing: Creating Shared Understanding
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
KodekX | Application Modernization Development
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Network Security Unit 5.pdf for BCA BBA.
20250228 LYD VKU AI Blended-Learning.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
The Rise and Fall of 3GPP – Time for a Sabbatical?
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
The AUB Centre for AI in Media Proposal.docx
Digital-Transformation-Roadmap-for-Companies.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Review of recent advances in non-invasive hemoglobin estimation
Encapsulation theory and applications.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Spectral efficient network and resource selection model in 5G networks
Unlocking AI with Model Context Protocol (MCP)

OO JS for AS3 Devs

  • 1. OO JS for AS3 Devs Object-Oriented JavaScript for ActionScript 3 Developers
  • 2. Pirates vs Ninjas Pirate Ninja
  • 3. JS vs AS3 Object Number String Boolean Object Object Array (Object)  Date Function Function Prototype <Script src=&quot;&quot;/> Function Object Number | int | unit String Boolean Array Dictionary Array Date Function Class Prototype Import Getter / Setter
  • 4. No Strong Types in JS var myVar1; var myVar1 = 10; var myVar1, myVar2, myVar3; var myVar = []; myVar.push(&quot;foo&quot;); const EVENT_NAME = &quot;name&quot; var myVar:Object public myVar public function myFunct():String function myFunct():String package {}, class {}  extends, implements, final, internal, public, private GOOD PUPPY  BAD PUPPY 
  • 5. 'Classes' in JS     Everything is an Object or Function Functions are pushed to the limits A JS Class is just a Function Function 'scope' rules are used for to create public, private spaces Inheritance is wacky ( will get to this later ) Prototype is wacky ( will get to this later )  
  • 6. function myClass() {      this.add = function(a, b)          {          return a + b;      }; } var c = new myClass(); c.add(1, 1); TEST IN FIREBUG
  • 7. Getters / Setters function Puppy() {      var _evil = 1000;      this. getEvil = function()      {          return _evil;      };      this. setEvil = function(value)      {          _evil  = value;      } } var evilPuppy = new Puppy(); evilPuppy. getEvil() ;
  • 8. Self Executing Functions function Puppy() {      var _evil = 999;      this.getEvil = function()      {          return _evil;      };      (function init(value)      {          _evil = v;      })(45) } var evilPuppy = new Puppy(); evilPuppy.getEvil();
  • 9. Scope is a Pain! function level_1() {      var good = 20;      this.evil = 99;      var timeout = setTimeout(function()          {              console.log(good);              console.log(evil);              console.log(this.evil);          }, 500); } var x = new level_1();
  • 10. Scope is a Pain - Part 2 function level_1() {      var good = 20;      this.evil = 99;      var scope = this;      var timeout = setTimeout(function()          {              console.log(good);              console.log(scope.evil);          }, 500); } var x = new level_1();
  • 11. Inheritance Tons of ways to do Inheritance  I will only cover one way No &quot;extends&quot; in JS Completely different way of thinking about it Special property on Object named Prototype
  • 12. function Animal() {      Animal.prototype.evil = 1000;      Animal.prototype.getColor = function()      {          return &quot;Blood Red&quot;;      } } function Puppy()  { } Puppy.prototype = Animal.prototype; var p = new Puppy(); p.getColor();
  • 13. function Animal() {      Animal.prototype.evil = 1000;      Animal.prototype.getColor = function()      {          return &quot;Blood Red&quot;;      } } function Puppy()  { } new Animal(); //(GOTCHA!) Puppy.prototype = Animal.prototype; var p = new Puppy(); p.getColor();
  • 14. function Animal(){      Animal.prototype.color = &quot;Blood Red&quot;;      Animal.prototype.getColor = function()      {          return &quot;Evil &quot; + this.color;      } }; var a = new Animal(); console.log(&quot;Animal Color: &quot; + a.getColor()); function Puppy() {      this.color = &quot;Blue&quot;; } Puppy.prototype = Animal.prototype; var p = new Puppy(); console.log(&quot;Puppy Color: &quot; + p.getColor());
  • 15. HTML5 <canvas> tag  <body> <canvas id=&quot;gameCanvas&quot; width=&quot;600&quot; height=&quot;600&quot;></canvas> </body>
  • 16. Get Pointer to <canvas> from JS // javaScript var canvas = document.getElementById('gameCanvas'); var context = canvas.getContext('2d');
  • 17. Draw on <canvas> with JS // javaScript context.beginPath(); context.moveTo(0,0); context.lineTo(0,100); context.lineTo(100,100); context.lineTo(100,0); context.closePath(); context.strokeStyle = &quot;#000000&quot;; context.fill(); context.fillStyle = &quot;#CCCCCC&quot;; context.fill();
  • 18. &quot;Animation&quot; (in quotes) var intervalId = setInterval(drawCanvas, 1000 / 30); function drawCanvas(){      // clear everything      context.clearRect(0,0, myWidth, myHeight);      // redraw everything      // Yes, you better know exactly       // where every pixel is at all times so       // you can redraw it at 30 FPS      // :( }
  • 19. &quot;Animation&quot; (in quotes) var intervalId = setInterval(smartDrawCanvas, 1000 / 30); function smartDrawCanvas() {      // Clear only part of the canvas      context.clearRect(25, 25, 50, 50);      // redraw SOME of the canvas      // TODO .... }
  • 20. Mouse Events on <canvas> Cannot listen to events on any children on the <canvas> Think of <canvas> as AS3 Bitmap You can only listen on the main <canvas> object You need to know where every 'interactive' object is the <canvas> at all times. For clicks you need to calculate what was under the pixel that was clicked on Observer pattern helps with this work
  • 21. Gotchas **JS** (for each works in FF, but not Chrome) for (var x in myArray) {      var obj = myArray[x];      obj.selected = false; } **AS3** for each (var x:Object in myArray) {      x.selected = false; }
  • 22. Gotchas var timeout = setTimeout(function()      {                  var x = passThruVar1;          // x is undefined!!!      }, 500, passThruVar1 , passThruVar2 );

Editor's Notes

  • #7: function myClass() {      this.add = function(a, b)          {          return a + b;      }; } var c = new