SlideShare une entreprise Scribd logo
1  sur  19
Télécharger pour lire hors ligne
Javascript - Avoiding the
                        bad parts

                                   Mikko Ohtamaa
                             mikko@opensourcehacker.com

                              http://opensourcehacker.com


                              http://twitter.com/moo9000

Saturday, November 5, 2011
Lighting talk of 5 min
                             Turn Python gurus to JS gurus

                                Plone Conference 2011




Saturday, November 5, 2011
Javascript has become
                              better
                             Don’t write Javascript like it was 1996




Saturday, November 5, 2011
What your every JS file
                    should look like


Saturday, November 5, 2011
/*global require,define,window,console*/
                   // Main
                   require(["jquery", "domready!",
                   "submodule"], function($, domready,
                   submodule) {
                       "use strict";

                             $("a").click(function() {
                                 var val = submodule.foobar();
                                 window.alert(val);
                             });
                   });




Saturday, November 5, 2011
/*global require,define,window,console*/
   // Submodule
   define(["jquery", "myothermodule"], function($, myothermodule) {
       'use strict';

             // Export public module API
             return {
                 foobar : function() {
                      return 1+1+myothermodule.func();
                 }
             };
   });




Saturday, November 5, 2011
Use require.js
                    • AMD (Asynchronous Module Definition)
                    • a.k.a. import for Javascript
                    • Dependency solving
                    • Automatic compression and merging of
                             needed and only needed JS files using
                             require.js optimizer


Saturday, November 5, 2011
Use ECMAScript5

                    • Goodies: this.function.bind(),
                             Array.forEach()
                    • Backwards compatibility (Internet
                             Explorer): http://bit.ly/es5shim



Saturday, November 5, 2011
“use strict”;
                   Enforces no global variables by default and fixes some
                               other bad language features




Saturday, November 5, 2011
Enforce background
                    JSLint checking in your
                          text editor
                             ... or JSLint modern fork, JSHint




Saturday, November 5, 2011
Saturday, November 5, 2011
// Some JSLint hinting

                /*global window,console*/
Saturday, November 5, 2011
Never use Javascript
                               inline in HTML


Saturday, November 5, 2011
<a href=”#”
                             onclick=”aargh()”>
                                     No




Saturday, November 5, 2011
// On page load
     $("a#handler").click(function() {
         // do stuff
     });

     // For dynamic content
     // (AJAX loaded)
     $("#handler").live(function() {
         // do stuff
     });
Saturday, November 5, 2011
Passing a bound
                             method to an event
                                handler using
                               function.bind()

Saturday, November 5, 2011
function FooClass(){
  }
  FooClass.prototype.myfunc = function(arg1,arg2) {
  }
  var f = new Foo();

  // Bind arguments are this, arg1, arg2
  setTimeout(f.myfunc.bind(f, "arg1", "arg2"), 10);




Saturday, November 5, 2011
Finding Javascript info

                    • Use Mozilla Developer Network: “MDN
                             Javascript bind”
                    • Put w3school.com on ban-list - only bad
                             information there




Saturday, November 5, 2011
Conclusion
                    • Worst JS farts have been fixed / can be
                             worked around with discipline
                    • Javascript still lacks syntatic sugar, is not
                             wrist friendly (boo!)
                    • Mozilla et. al. are working on to fix that
                             (but Philip didn’t promise do drop curly
                             brackets or semicolons)


Saturday, November 5, 2011

Contenu connexe

Similaire à Avoid bad Javascript with RequireJS, ECMAScript5 and strict mode

Conquistando el Servidor con Node.JS
Conquistando el Servidor con Node.JSConquistando el Servidor con Node.JS
Conquistando el Servidor con Node.JSCaridy Patino
 
잘 알려지지 않은 Php 코드 활용하기
잘 알려지지 않은 Php 코드 활용하기잘 알려지지 않은 Php 코드 활용하기
잘 알려지지 않은 Php 코드 활용하기형우 안
 
international PHP2011_ilia alshanetsky_Hidden Features of PHP
international PHP2011_ilia alshanetsky_Hidden Features of PHPinternational PHP2011_ilia alshanetsky_Hidden Features of PHP
international PHP2011_ilia alshanetsky_Hidden Features of PHPsmueller_sandsmedia
 
3D in the Browser via WebGL: It's Go Time
3D in the Browser via WebGL: It's Go Time 3D in the Browser via WebGL: It's Go Time
3D in the Browser via WebGL: It's Go Time Pascal Rettig
 
TorqueBox - Ultrapassando a fronteira entre Java e Ruby
TorqueBox - Ultrapassando a fronteira entre Java e RubyTorqueBox - Ultrapassando a fronteira entre Java e Ruby
TorqueBox - Ultrapassando a fronteira entre Java e RubyBruno Oliveira
 
Social design (Seattle 09-2011)
Social design (Seattle 09-2011)Social design (Seattle 09-2011)
Social design (Seattle 09-2011)Andrei Zyuzikov
 
Writing jQuery that doesn't suck - London jQuery
Writing jQuery that doesn't suck - London jQueryWriting jQuery that doesn't suck - London jQuery
Writing jQuery that doesn't suck - London jQueryRoss Bruniges
 
The Solar Framework for PHP
The Solar Framework for PHPThe Solar Framework for PHP
The Solar Framework for PHPConFoo
 
Moving to Dojo 1.7 and the path to 2.0
Moving to Dojo 1.7 and the path to 2.0Moving to Dojo 1.7 and the path to 2.0
Moving to Dojo 1.7 and the path to 2.0James Thomas
 
Javascript Performance
Javascript PerformanceJavascript Performance
Javascript Performanceolivvv
 
Javascript Basics - part 1
Javascript Basics - part 1Javascript Basics - part 1
Javascript Basics - part 1Kasia Drzyzga
 
WebGL Fundamentals
WebGL FundamentalsWebGL Fundamentals
WebGL FundamentalsSencha
 
Web Scraping using Diazo!
Web Scraping using Diazo!Web Scraping using Diazo!
Web Scraping using Diazo!pythonchile
 
Puppet camp europe 2011 hackability
Puppet camp europe 2011   hackabilityPuppet camp europe 2011   hackability
Puppet camp europe 2011 hackabilityPuppet
 
Symony2 A Next Generation PHP Framework
Symony2 A Next Generation PHP FrameworkSymony2 A Next Generation PHP Framework
Symony2 A Next Generation PHP FrameworkRyan Weaver
 
Pluggable Django Application Patterns PyCon 2011
Pluggable Django Application Patterns PyCon 2011Pluggable Django Application Patterns PyCon 2011
Pluggable Django Application Patterns PyCon 2011Corey Oordt
 
The Fast, The Slow and the Lazy
The Fast, The Slow and the LazyThe Fast, The Slow and the Lazy
The Fast, The Slow and the LazyMaurício Linhares
 

Similaire à Avoid bad Javascript with RequireJS, ECMAScript5 and strict mode (20)

Caridy patino - node-js
Caridy patino - node-jsCaridy patino - node-js
Caridy patino - node-js
 
Conquistando el Servidor con Node.JS
Conquistando el Servidor con Node.JSConquistando el Servidor con Node.JS
Conquistando el Servidor con Node.JS
 
잘 알려지지 않은 Php 코드 활용하기
잘 알려지지 않은 Php 코드 활용하기잘 알려지지 않은 Php 코드 활용하기
잘 알려지지 않은 Php 코드 활용하기
 
international PHP2011_ilia alshanetsky_Hidden Features of PHP
international PHP2011_ilia alshanetsky_Hidden Features of PHPinternational PHP2011_ilia alshanetsky_Hidden Features of PHP
international PHP2011_ilia alshanetsky_Hidden Features of PHP
 
Pocket Knife JS
Pocket Knife JSPocket Knife JS
Pocket Knife JS
 
Splash
SplashSplash
Splash
 
3D in the Browser via WebGL: It's Go Time
3D in the Browser via WebGL: It's Go Time 3D in the Browser via WebGL: It's Go Time
3D in the Browser via WebGL: It's Go Time
 
TorqueBox - Ultrapassando a fronteira entre Java e Ruby
TorqueBox - Ultrapassando a fronteira entre Java e RubyTorqueBox - Ultrapassando a fronteira entre Java e Ruby
TorqueBox - Ultrapassando a fronteira entre Java e Ruby
 
Social design (Seattle 09-2011)
Social design (Seattle 09-2011)Social design (Seattle 09-2011)
Social design (Seattle 09-2011)
 
Writing jQuery that doesn't suck - London jQuery
Writing jQuery that doesn't suck - London jQueryWriting jQuery that doesn't suck - London jQuery
Writing jQuery that doesn't suck - London jQuery
 
The Solar Framework for PHP
The Solar Framework for PHPThe Solar Framework for PHP
The Solar Framework for PHP
 
Moving to Dojo 1.7 and the path to 2.0
Moving to Dojo 1.7 and the path to 2.0Moving to Dojo 1.7 and the path to 2.0
Moving to Dojo 1.7 and the path to 2.0
 
Javascript Performance
Javascript PerformanceJavascript Performance
Javascript Performance
 
Javascript Basics - part 1
Javascript Basics - part 1Javascript Basics - part 1
Javascript Basics - part 1
 
WebGL Fundamentals
WebGL FundamentalsWebGL Fundamentals
WebGL Fundamentals
 
Web Scraping using Diazo!
Web Scraping using Diazo!Web Scraping using Diazo!
Web Scraping using Diazo!
 
Puppet camp europe 2011 hackability
Puppet camp europe 2011   hackabilityPuppet camp europe 2011   hackability
Puppet camp europe 2011 hackability
 
Symony2 A Next Generation PHP Framework
Symony2 A Next Generation PHP FrameworkSymony2 A Next Generation PHP Framework
Symony2 A Next Generation PHP Framework
 
Pluggable Django Application Patterns PyCon 2011
Pluggable Django Application Patterns PyCon 2011Pluggable Django Application Patterns PyCon 2011
Pluggable Django Application Patterns PyCon 2011
 
The Fast, The Slow and the Lazy
The Fast, The Slow and the LazyThe Fast, The Slow and the Lazy
The Fast, The Slow and the Lazy
 

Plus de Mikko Ohtamaa

Websauna - introduction to the best Python web framework
Websauna - introduction to the best Python web frameworkWebsauna - introduction to the best Python web framework
Websauna - introduction to the best Python web frameworkMikko Ohtamaa
 
Operations Security - SF Bitcoin Hackday March 2015
Operations Security - SF Bitcoin Hackday March 2015Operations Security - SF Bitcoin Hackday March 2015
Operations Security - SF Bitcoin Hackday March 2015Mikko Ohtamaa
 
Operations security - SyPy Dec 2014 (Sydney Python users)
Operations security - SyPy Dec 2014 (Sydney Python users)Operations security - SyPy Dec 2014 (Sydney Python users)
Operations security - SyPy Dec 2014 (Sydney Python users)Mikko Ohtamaa
 
Operations security (OPSEC)
Operations security (OPSEC)Operations security (OPSEC)
Operations security (OPSEC)Mikko Ohtamaa
 
Plone, battle-scarred community with battle tanks
Plone, battle-scarred community with battle tanksPlone, battle-scarred community with battle tanks
Plone, battle-scarred community with battle tanksMikko Ohtamaa
 
World Plone Day 2013
World Plone Day 2013World Plone Day 2013
World Plone Day 2013Mikko Ohtamaa
 
Solving problems one Plone package at a time
Solving problems one Plone package at a timeSolving problems one Plone package at a time
Solving problems one Plone package at a timeMikko Ohtamaa
 
Saving Plone from Plone agony
Saving Plone from Plone agonySaving Plone from Plone agony
Saving Plone from Plone agonyMikko Ohtamaa
 
Beautiful Maintainable ModularJavascript Codebase with RequireJS - HelsinkiJ...
 Beautiful Maintainable ModularJavascript Codebase with RequireJS - HelsinkiJ... Beautiful Maintainable ModularJavascript Codebase with RequireJS - HelsinkiJ...
Beautiful Maintainable ModularJavascript Codebase with RequireJS - HelsinkiJ...Mikko Ohtamaa
 
VVV validation and linting tool
VVV validation and linting toolVVV validation and linting tool
VVV validation and linting toolMikko Ohtamaa
 
Plone IDE - the future of Plone development
Plone IDE - the future of Plone developmentPlone IDE - the future of Plone development
Plone IDE - the future of Plone developmentMikko Ohtamaa
 
The Easy Way - Plone Conference 2011
The Easy Way - Plone Conference 2011The Easy Way - Plone Conference 2011
The Easy Way - Plone Conference 2011Mikko Ohtamaa
 
Mobile Landscape 2011
Mobile Landscape 2011Mobile Landscape 2011
Mobile Landscape 2011Mikko Ohtamaa
 
Mobiilimarkkinoinnin mahdollisuudet nyt
Mobiilimarkkinoinnin mahdollisuudet nytMobiilimarkkinoinnin mahdollisuudet nyt
Mobiilimarkkinoinnin mahdollisuudet nytMikko Ohtamaa
 
The World Outside Plone
The World Outside PloneThe World Outside Plone
The World Outside PloneMikko Ohtamaa
 
mFabrik Case Studies
mFabrik Case StudiesmFabrik Case Studies
mFabrik Case StudiesMikko Ohtamaa
 
Building HTML based mobile phone applications
Building HTML based mobile phone applicationsBuilding HTML based mobile phone applications
Building HTML based mobile phone applicationsMikko Ohtamaa
 

Plus de Mikko Ohtamaa (19)

Websauna - introduction to the best Python web framework
Websauna - introduction to the best Python web frameworkWebsauna - introduction to the best Python web framework
Websauna - introduction to the best Python web framework
 
Operations Security - SF Bitcoin Hackday March 2015
Operations Security - SF Bitcoin Hackday March 2015Operations Security - SF Bitcoin Hackday March 2015
Operations Security - SF Bitcoin Hackday March 2015
 
Operations security - SyPy Dec 2014 (Sydney Python users)
Operations security - SyPy Dec 2014 (Sydney Python users)Operations security - SyPy Dec 2014 (Sydney Python users)
Operations security - SyPy Dec 2014 (Sydney Python users)
 
Operations security (OPSEC)
Operations security (OPSEC)Operations security (OPSEC)
Operations security (OPSEC)
 
Plone, battle-scarred community with battle tanks
Plone, battle-scarred community with battle tanksPlone, battle-scarred community with battle tanks
Plone, battle-scarred community with battle tanks
 
World Plone Day 2013
World Plone Day 2013World Plone Day 2013
World Plone Day 2013
 
Test lol
Test lolTest lol
Test lol
 
Writing the docs
Writing the docsWriting the docs
Writing the docs
 
Solving problems one Plone package at a time
Solving problems one Plone package at a timeSolving problems one Plone package at a time
Solving problems one Plone package at a time
 
Saving Plone from Plone agony
Saving Plone from Plone agonySaving Plone from Plone agony
Saving Plone from Plone agony
 
Beautiful Maintainable ModularJavascript Codebase with RequireJS - HelsinkiJ...
 Beautiful Maintainable ModularJavascript Codebase with RequireJS - HelsinkiJ... Beautiful Maintainable ModularJavascript Codebase with RequireJS - HelsinkiJ...
Beautiful Maintainable ModularJavascript Codebase with RequireJS - HelsinkiJ...
 
VVV validation and linting tool
VVV validation and linting toolVVV validation and linting tool
VVV validation and linting tool
 
Plone IDE - the future of Plone development
Plone IDE - the future of Plone developmentPlone IDE - the future of Plone development
Plone IDE - the future of Plone development
 
The Easy Way - Plone Conference 2011
The Easy Way - Plone Conference 2011The Easy Way - Plone Conference 2011
The Easy Way - Plone Conference 2011
 
Mobile Landscape 2011
Mobile Landscape 2011Mobile Landscape 2011
Mobile Landscape 2011
 
Mobiilimarkkinoinnin mahdollisuudet nyt
Mobiilimarkkinoinnin mahdollisuudet nytMobiilimarkkinoinnin mahdollisuudet nyt
Mobiilimarkkinoinnin mahdollisuudet nyt
 
The World Outside Plone
The World Outside PloneThe World Outside Plone
The World Outside Plone
 
mFabrik Case Studies
mFabrik Case StudiesmFabrik Case Studies
mFabrik Case Studies
 
Building HTML based mobile phone applications
Building HTML based mobile phone applicationsBuilding HTML based mobile phone applications
Building HTML based mobile phone applications
 

Dernier

Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 

Dernier (20)

Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 

Avoid bad Javascript with RequireJS, ECMAScript5 and strict mode

  • 1. Javascript - Avoiding the bad parts Mikko Ohtamaa mikko@opensourcehacker.com http://opensourcehacker.com http://twitter.com/moo9000 Saturday, November 5, 2011
  • 2. Lighting talk of 5 min Turn Python gurus to JS gurus Plone Conference 2011 Saturday, November 5, 2011
  • 3. Javascript has become better Don’t write Javascript like it was 1996 Saturday, November 5, 2011
  • 4. What your every JS file should look like Saturday, November 5, 2011
  • 5. /*global require,define,window,console*/ // Main require(["jquery", "domready!", "submodule"], function($, domready, submodule) { "use strict"; $("a").click(function() { var val = submodule.foobar(); window.alert(val); }); }); Saturday, November 5, 2011
  • 6. /*global require,define,window,console*/ // Submodule define(["jquery", "myothermodule"], function($, myothermodule) { 'use strict'; // Export public module API return { foobar : function() { return 1+1+myothermodule.func(); } }; }); Saturday, November 5, 2011
  • 7. Use require.js • AMD (Asynchronous Module Definition) • a.k.a. import for Javascript • Dependency solving • Automatic compression and merging of needed and only needed JS files using require.js optimizer Saturday, November 5, 2011
  • 8. Use ECMAScript5 • Goodies: this.function.bind(), Array.forEach() • Backwards compatibility (Internet Explorer): http://bit.ly/es5shim Saturday, November 5, 2011
  • 9. “use strict”; Enforces no global variables by default and fixes some other bad language features Saturday, November 5, 2011
  • 10. Enforce background JSLint checking in your text editor ... or JSLint modern fork, JSHint Saturday, November 5, 2011
  • 12. // Some JSLint hinting /*global window,console*/ Saturday, November 5, 2011
  • 13. Never use Javascript inline in HTML Saturday, November 5, 2011
  • 14. <a href=”#” onclick=”aargh()”> No Saturday, November 5, 2011
  • 15. // On page load $("a#handler").click(function() { // do stuff }); // For dynamic content // (AJAX loaded) $("#handler").live(function() { // do stuff }); Saturday, November 5, 2011
  • 16. Passing a bound method to an event handler using function.bind() Saturday, November 5, 2011
  • 17. function FooClass(){ } FooClass.prototype.myfunc = function(arg1,arg2) { } var f = new Foo(); // Bind arguments are this, arg1, arg2 setTimeout(f.myfunc.bind(f, "arg1", "arg2"), 10); Saturday, November 5, 2011
  • 18. Finding Javascript info • Use Mozilla Developer Network: “MDN Javascript bind” • Put w3school.com on ban-list - only bad information there Saturday, November 5, 2011
  • 19. Conclusion • Worst JS farts have been fixed / can be worked around with discipline • Javascript still lacks syntatic sugar, is not wrist friendly (boo!) • Mozilla et. al. are working on to fix that (but Philip didn’t promise do drop curly brackets or semicolons) Saturday, November 5, 2011