SlideShare a Scribd company logo
Khan Academy
Computer Science
     John Resig (ejohn.org)
   http://khanacademy.org/cs
Inspiration


• Bret Victor
 • http://vimeo.com/36579366
Learning to Program

• Learn through hands-on experimentation
• Learn by looking at other people’s code
• Learn by building on other people’s code
CS Projects
                    “Forked”     New



• “Forked”: 62930
• New: 22225
                    26%



• 1.86m Views                  74%
Github in Disguise

• Forking (Hitting “Save as...”)
• Versioning (Not yet user exposed)
• Runtime Versioning
 • All runtime changes are versioned to
    protect against API changes
Implementation

• Graphics: Processing.js
• Real-time Execution
 • Dynamic Injection
 • Error handling
Graphics

• Use Processing.js’ API
• Works in all browsers with <canvas>
• Import it wholesale, ignore the “Processing
  language” bits
Real-Time Execution

• Code is constantly checked for changes
• If a change is detected, code is re-evaluated
• Code is not run using a simple “eval”, code
  is dynamically injected into the current
  runtime.
How Code is Injected
•   JSHint (run in worker thread)
    •   Optionally run BabyHint
    •   If Error, stop execution
•   Cache Images
•   Run Code in Worker Thread, Looking for long-running code
    •   If error, stop execution
•   Execute Code
    •   If first time running code, just eval
    •   If later:
        •   Eval code and extract current state
        •   Inject extracted values into runtime
        •   Maintain closures with functions!
Error Messages


• All code is run through JSHint
• And through an extra layer of error
  handling (called “BabyHint”)
JSHint
• Validation!
• Also: Gives us a list of all global variables
  used in the program
• Compare against a master list of properties
  in Processing.js
• Everything else is user-defined!
BabyHint
•   Handles common mistakes:
    •   Spelling and case: “strokeWeight” vs.
        “strokeweight”
    •   Gives sane errors about missing semicolons
    •   Provide hints about correct function arguments
        strokeWeight(); (gives an error asking for more
        args)
    •   Check for function declaration mistakes and
        possible spacing mistakes (“vartest”)
Worker Threads

• Run JavaScript code asynchronously in the
  background of a page
• Available in Chrome, Firefox, Safari, and IE
  10 (Need to make sure it works in IE 9!)
• Works by doing a string-only postMessage
  to the worker and waiting for a response
With Statements
• var obj = { name: “John”, city: “Boston” };
  with (obj) {
    name += “ Resig”;
    city = “Brooklyn”;
  }
• obj.name === “John Resig”
  obj.city === “Brooklyn”
With Statements
• var obj = { name: “John”, city: “Boston” };
  with (obj) {
    var city = “Brooklyn”;
    var job = “Khan Academy”;
  }
• obj.city === “Brooklyn”
  obj.job === undefined
With Statements
• var obj = { name: “John”, city: “Boston” };
  obj.job = undefined;
  with (obj) {
    var city = “Brooklyn”;
    var job = “Khan Academy”;
  }
• obj.city === “Brooklyn”
  obj.job === “Khan Academy”
Example
•   var x = 5, y = 1;
    var draw = function() {
       x += y;
    };
•   JSHint: PASS
•   Long Run Detection: PASS
•   First Run: True
    •   Code is evaluated
    •   lastGrab = {
           x: “5”,
           y: “1”,
           draw: “function() { x += y; }”
        }
Example (cont.)
•   var x = 50, y = 1;
    var draw = function() {
       x += y;
    };

•   JSHint: PASS
•   Long Run Detection: PASS

•   First Run: False
    •   Code is evaluated

    •   grabAll = {
           x: “50”,
           y: “1”,
           draw: “function() { x += y; }”
        }
    •   Compare with lastGrab: grabAll.x !== lastGrab.x

    •   Eval: “var x = 50;”
Example (cont.)
•   var x = 50, y = 1;
    var draw = function() {
       x += y * 2;
    };

•   JSHint: PASS
•   Long Run Detection: PASS

•   First Run: False
    •   Code is evaluated

    •   grabAll = {
           x: “50”,
           y: “1”,
           draw: “function() { x += y * 2; }”
        }
    •   Compare with lastGrab: grabAll.draw !== lastGrab.draw

    •   Eval: “var draw = function() { x += y * 2; };”

More Related Content

PDF
JavaScript Language Update 2016 (LLoT)
PDF
使用.NET构建轻量级分布式框架
PDF
Dmytro Kochergin Angular 2 and New Java Script Technologies
PDF
LINQ Inside
PDF
How to-node-core
PDF
JavaScript For CSharp Developer
PDF
JavaScript
PDF
Node.js Patterns and Opinions
JavaScript Language Update 2016 (LLoT)
使用.NET构建轻量级分布式框架
Dmytro Kochergin Angular 2 and New Java Script Technologies
LINQ Inside
How to-node-core
JavaScript For CSharp Developer
JavaScript
Node.js Patterns and Opinions

What's hot (17)

PDF
Using JS to teach JS at Khan Academy
PDF
Fighting Ruby code smell
PDF
java8-patterns
PDF
«Управление логикой переходов между экранами приложения с помощью координатор...
PPTX
I18nize Scala programs à la gettext
PPT
RubyMotion #jbday
PDF
What's new in PHP 7.1
PDF
Essential ElixirScript - Roman Senin
PDF
Open World Forum 2014 : From ES6 to Javascript 2.0. What use today ? par Jon...
PDF
Elm architecture
PDF
LuaNode: Asynchronous I/O for Lua
PDF
Angular 2.0: Brighter future?
PDF
ECMAScript 6
PDF
Testing swagger contracts without contract based testing
PDF
ORM vs GraphQL - Python fwdays 2019
PDF
Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria
PDF
WDB005.1 - JavaScript for Java Developers (Lecture 1)
Using JS to teach JS at Khan Academy
Fighting Ruby code smell
java8-patterns
«Управление логикой переходов между экранами приложения с помощью координатор...
I18nize Scala programs à la gettext
RubyMotion #jbday
What's new in PHP 7.1
Essential ElixirScript - Roman Senin
Open World Forum 2014 : From ES6 to Javascript 2.0. What use today ? par Jon...
Elm architecture
LuaNode: Asynchronous I/O for Lua
Angular 2.0: Brighter future?
ECMAScript 6
Testing swagger contracts without contract based testing
ORM vs GraphQL - Python fwdays 2019
Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria
WDB005.1 - JavaScript for Java Developers (Lecture 1)
Ad

Viewers also liked (10)

PDF
jQuery Internals + Cool Stuff
PDF
JavaScript Libraries (Ajax Exp 2006)
PDF
JavaScript Libraries (Kings of Code)
PDF
Processing and Processing.js
PDF
Secrets of JavaScript Libraries
PDF
Testing Mobile JavaScript (Fall 2010
PDF
Building a JavaScript Library
PDF
Understanding JavaScript Testing
PDF
Crafting Visual Stories with Data
PDF
Geração Automática de Ontologias Probabilíticas a partir de um modelo UMP-ST
jQuery Internals + Cool Stuff
JavaScript Libraries (Ajax Exp 2006)
JavaScript Libraries (Kings of Code)
Processing and Processing.js
Secrets of JavaScript Libraries
Testing Mobile JavaScript (Fall 2010
Building a JavaScript Library
Understanding JavaScript Testing
Crafting Visual Stories with Data
Geração Automática de Ontologias Probabilíticas a partir de um modelo UMP-ST
Ad

Similar to Khan Academy Computer Science (20)

PDF
Surviving javascript.pptx
PPTX
JavaScript (without DOM)
PDF
JavaScript Good Practices
PDF
Practical JavaScript Programming - Session 7/8
KEY
JavaScript Neednt Hurt - JavaBin talk
PDF
JavaScript The Definitive Guide 7th Edition David Flanagan
PPT
Javascript
PPTX
JavaScript Proven Practises
PPTX
Security testing of YUI powered applications
PDF
JSHint: Learning JavaScript the Hard Way
PDF
Douglas Crockford: Serversideness
KEY
谈谈Javascript设计
KEY
谈谈Javascript设计
PDF
l2-es6-160830040119.pdf
PDF
Lecture 2: ES6 / ES2015 Slide
PPTX
Javascript best practices
PDF
Rails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
PPTX
Javascript
PPT
JavaScript Misunderstood
PPTX
Functional Programming in Javascript - IL Tech Talks week
Surviving javascript.pptx
JavaScript (without DOM)
JavaScript Good Practices
Practical JavaScript Programming - Session 7/8
JavaScript Neednt Hurt - JavaBin talk
JavaScript The Definitive Guide 7th Edition David Flanagan
Javascript
JavaScript Proven Practises
Security testing of YUI powered applications
JSHint: Learning JavaScript the Hard Way
Douglas Crockford: Serversideness
谈谈Javascript设计
谈谈Javascript设计
l2-es6-160830040119.pdf
Lecture 2: ES6 / ES2015 Slide
Javascript best practices
Rails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
Javascript
JavaScript Misunderstood
Functional Programming in Javascript - IL Tech Talks week

More from jeresig (20)

PDF
Does Coding Every Day Matter?
PDF
Accidentally Becoming a Digital Librarian
PDF
2014: John's Favorite Thing (Neo4j)
PDF
Computer Vision as Art Historical Investigation
PDF
Hacking Art History
PDF
Applying Computer Vision to Art History
PDF
NYARC 2014: Frick/Zeri Results
PDF
EmpireJS: Hacking Art with Node js and Image Analysis
PDF
Applying Computer Vision to Art History
PDF
Introduction to jQuery (Ajax Exp 2006)
PDF
jQuery Recommendations to the W3C (2011)
PDF
jQuery Open Source Process (RIT 2011)
PDF
jQuery Open Source Process (Knight Foundation 2011)
PDF
jQuery Mobile
PDF
jQuery Open Source (Fronteer 2011)
PDF
Holistic JavaScript Performance
PDF
New Features Coming in Browsers (RIT '09)
PDF
Introduction to jQuery (Ajax Exp 2007)
PDF
Advanced jQuery (Ajax Exp 2007)
PDF
JavaScript Library Overview (Ajax Exp West 2007)
Does Coding Every Day Matter?
Accidentally Becoming a Digital Librarian
2014: John's Favorite Thing (Neo4j)
Computer Vision as Art Historical Investigation
Hacking Art History
Applying Computer Vision to Art History
NYARC 2014: Frick/Zeri Results
EmpireJS: Hacking Art with Node js and Image Analysis
Applying Computer Vision to Art History
Introduction to jQuery (Ajax Exp 2006)
jQuery Recommendations to the W3C (2011)
jQuery Open Source Process (RIT 2011)
jQuery Open Source Process (Knight Foundation 2011)
jQuery Mobile
jQuery Open Source (Fronteer 2011)
Holistic JavaScript Performance
New Features Coming in Browsers (RIT '09)
Introduction to jQuery (Ajax Exp 2007)
Advanced jQuery (Ajax Exp 2007)
JavaScript Library Overview (Ajax Exp West 2007)

Recently uploaded (20)

PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Modernizing your data center with Dell and AMD
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
A Presentation on Artificial Intelligence
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Machine learning based COVID-19 study performance prediction
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Big Data Technologies - Introduction.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
KodekX | Application Modernization Development
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Network Security Unit 5.pdf for BCA BBA.
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
NewMind AI Monthly Chronicles - July 2025
Modernizing your data center with Dell and AMD
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Encapsulation_ Review paper, used for researhc scholars
NewMind AI Weekly Chronicles - August'25 Week I
A Presentation on Artificial Intelligence
Advanced methodologies resolving dimensionality complications for autism neur...
Machine learning based COVID-19 study performance prediction
Review of recent advances in non-invasive hemoglobin estimation
Big Data Technologies - Introduction.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
The AUB Centre for AI in Media Proposal.docx
KodekX | Application Modernization Development
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Digital-Transformation-Roadmap-for-Companies.pptx

Khan Academy Computer Science

  • 1. Khan Academy Computer Science John Resig (ejohn.org) http://khanacademy.org/cs
  • 2. Inspiration • Bret Victor • http://vimeo.com/36579366
  • 3. Learning to Program • Learn through hands-on experimentation • Learn by looking at other people’s code • Learn by building on other people’s code
  • 4. CS Projects “Forked” New • “Forked”: 62930 • New: 22225 26% • 1.86m Views 74%
  • 5. Github in Disguise • Forking (Hitting “Save as...”) • Versioning (Not yet user exposed) • Runtime Versioning • All runtime changes are versioned to protect against API changes
  • 6. Implementation • Graphics: Processing.js • Real-time Execution • Dynamic Injection • Error handling
  • 7. Graphics • Use Processing.js’ API • Works in all browsers with <canvas> • Import it wholesale, ignore the “Processing language” bits
  • 8. Real-Time Execution • Code is constantly checked for changes • If a change is detected, code is re-evaluated • Code is not run using a simple “eval”, code is dynamically injected into the current runtime.
  • 9. How Code is Injected • JSHint (run in worker thread) • Optionally run BabyHint • If Error, stop execution • Cache Images • Run Code in Worker Thread, Looking for long-running code • If error, stop execution • Execute Code • If first time running code, just eval • If later: • Eval code and extract current state • Inject extracted values into runtime • Maintain closures with functions!
  • 10. Error Messages • All code is run through JSHint • And through an extra layer of error handling (called “BabyHint”)
  • 11. JSHint • Validation! • Also: Gives us a list of all global variables used in the program • Compare against a master list of properties in Processing.js • Everything else is user-defined!
  • 12. BabyHint • Handles common mistakes: • Spelling and case: “strokeWeight” vs. “strokeweight” • Gives sane errors about missing semicolons • Provide hints about correct function arguments strokeWeight(); (gives an error asking for more args) • Check for function declaration mistakes and possible spacing mistakes (“vartest”)
  • 13. Worker Threads • Run JavaScript code asynchronously in the background of a page • Available in Chrome, Firefox, Safari, and IE 10 (Need to make sure it works in IE 9!) • Works by doing a string-only postMessage to the worker and waiting for a response
  • 14. With Statements • var obj = { name: “John”, city: “Boston” }; with (obj) { name += “ Resig”; city = “Brooklyn”; } • obj.name === “John Resig” obj.city === “Brooklyn”
  • 15. With Statements • var obj = { name: “John”, city: “Boston” }; with (obj) { var city = “Brooklyn”; var job = “Khan Academy”; } • obj.city === “Brooklyn” obj.job === undefined
  • 16. With Statements • var obj = { name: “John”, city: “Boston” }; obj.job = undefined; with (obj) { var city = “Brooklyn”; var job = “Khan Academy”; } • obj.city === “Brooklyn” obj.job === “Khan Academy”
  • 17. Example • var x = 5, y = 1; var draw = function() { x += y; }; • JSHint: PASS • Long Run Detection: PASS • First Run: True • Code is evaluated • lastGrab = { x: “5”, y: “1”, draw: “function() { x += y; }” }
  • 18. Example (cont.) • var x = 50, y = 1; var draw = function() { x += y; }; • JSHint: PASS • Long Run Detection: PASS • First Run: False • Code is evaluated • grabAll = { x: “50”, y: “1”, draw: “function() { x += y; }” } • Compare with lastGrab: grabAll.x !== lastGrab.x • Eval: “var x = 50;”
  • 19. Example (cont.) • var x = 50, y = 1; var draw = function() { x += y * 2; }; • JSHint: PASS • Long Run Detection: PASS • First Run: False • Code is evaluated • grabAll = { x: “50”, y: “1”, draw: “function() { x += y * 2; }” } • Compare with lastGrab: grabAll.draw !== lastGrab.draw • Eval: “var draw = function() { x += y * 2; };”