SlideShare a Scribd company logo
Javascript
~ Beyond jQuery() ~
Project: Content.js
Variables
// string variable
var title = "Content.js Yo!";
// jQuery object
var $body = $("body");
// Global variable
window.$body = $body;
// Initialize an object
var contentSettings = {
titleEl: 'h1',
sectionNameEl: 'h3',
color: "blue"
};
// Initialize an array
var sectionNames = ["Section 1", "Section 2"];
var sectionContent = ["Section 1 content...", "Section 2 content..."];
Functions
// Standard function
function init(title) {
jQuery("body").append(title);
}
// Anonymous function
(function(title) {
jQuery("body").append(title);
})();
<script>
// string variable
var title = "Content.js Yo!";
// jQuery object
var $body = $("body");
// Global variable
window.$body = $body;
// Initialize an object
var contentSettings = {
titleEl: 'h1',
sectionNameEl: 'h3',
color: "blue"
};
// Initialize an array
var sectionNames = ["Section 1", "Section 2"];
var sectionContent = ["Section 1 content...", "Section 2 content..."];
function init(title) {
jQuery("body").append(title);
};
init(title);
</script>
Scope
All of these variables and the
function are all in the global
namespace, which could cause
conflicts.
<script>
(function($) {
// string variable
var title = "Content.js Yo!";
// jQuery object
var $body = $("body");
// Global variable
window.$body = $body;
// Initialize an object
var contentSettings = {
titleEl: 'h1',
sectionNameEl: 'h3',
color: "blue"
};
// Initialize an array
var sectionNames = ["Section 1", "Section 2"];
var sectionContent = ["Section 1 content...", "Section 2 content..."];
wcpdxInit(title);
})(jQuery);
function wcpdxInit(title) {
window.$body.html(title);
}
</script>
Scope
So let's wrap our variables in an
anonymous function and
namespace our function.
var $tab = jQuery('.section');
$tab.fadeIn();
$tab.addClass('active');
////////
// vs
////////
jQuery('.section').fadeIn();
jQuery('.section').addClass('active');
Caching
Caching DOM queries can be
a great way to make your
code more performant, but
be careful!
Mind Break
<script>
(function($) {
// Initialize an object
var contentSettings = {
titleEl: 'h1',
sectionNameEl: 'h3',
color: "blue",
title: 'Content.js Yo!',
sectionNames: ["Section 1", "Section 2"],
sectionContent: ["Section 1 content...", "Section 2 content..."]
};
wcpdxInit(contentSettings, $);
})(jQuery);
function wcpdxInit(settings, $) {
var $heading = $(document.createElement(settings.titleEl));
var $body = $('body');
var i = 0;
// Set the title
$body.html($heading.html(settings.title));
// loop through all of our content
for(i = -1; i < settings.sectionNames.length; i ++) {
// Create our DOM elements
var $sectionHeading = $(document.createElement(settings.sectionNameEl));
var $sectionContent = $(document.createElement('p')).css('color', settings.color);
$sectionHeading.html(settings.sectionNames[i]);
$body.append($sectionHeading);
$body.append($sectionContent.html(settings.sectionContent[i]));
}
}
</script>
Final
Name
That
Browser!
Debugging
The Console
Use "console.log( ... )" to print things from your script to
the browser console.
Breakpoints
Add a break point to the Source in the browser console,
or add "debugger;" to your script.
Minified Files
Resources:
api.jquery.com
jsperf.com
developer.mozilla.org
codecademy.com
caniuse.com
Tanner Moushey
@tannermoushey
iwitnessdesign.com
tannermoushey.com

More Related Content

What's hot

Drupal.Behaviors
Drupal.BehaviorsDrupal.Behaviors
Drupal.Behaviors
Tom Friedhof
 
J querypractice
J querypracticeJ querypractice
J querypractice
Inbal Geffen
 
Javascript ch8
Javascript ch8Javascript ch8
Javascript ch8
Brady Cheng
 
Java script
Java scriptJava script
Introducing AngularJS
Introducing AngularJSIntroducing AngularJS
Introducing AngularJS
Loc Nguyen
 
Evented Javascript
Evented JavascriptEvented Javascript
Evented Javascript
waw325
 
JQuery
JQueryJQuery
JQuery
Jacob Nelson
 
Grails UI Primer
Grails UI PrimerGrails UI Primer
Grails UI Primer
Matthew Taylor
 
‎Unit tests automation for legacy code‎ at YAPC::NA 2016
‎Unit tests automation for legacy code‎ at YAPC::NA 2016‎Unit tests automation for legacy code‎ at YAPC::NA 2016
‎Unit tests automation for legacy code‎ at YAPC::NA 2016
Denis Shirokov
 
J Query Public
J Query PublicJ Query Public
J Query Public
pradeepsilamkoti
 
A single language for backend and frontend from AngularJS to cloud with Clau...
A single language for backend and frontend  from AngularJS to cloud with Clau...A single language for backend and frontend  from AngularJS to cloud with Clau...
A single language for backend and frontend from AngularJS to cloud with Clau...
Walter Dal Mut
 
So cal0365productivitygroup feb2019
So cal0365productivitygroup feb2019So cal0365productivitygroup feb2019
So cal0365productivitygroup feb2019
RonRohlfs1
 
J query1
J query1J query1
J query1
Manav Prasad
 
jQuery : Events are where it happens!
jQuery : Events are where it happens!jQuery : Events are where it happens!
jQuery : Events are where it happens!
Wildan Maulana
 
J query
J queryJ query
J query
Manav Prasad
 
XQDT - XQuery Getting Momentum in Eclipse
XQDT - XQuery Getting Momentum in EclipseXQDT - XQuery Getting Momentum in Eclipse
XQDT - XQuery Getting Momentum in Eclipse
guesteb3ec2
 
Net/http and the http.handler interface
Net/http and the http.handler interfaceNet/http and the http.handler interface
Net/http and the http.handler interface
Joakim Gustin
 
Fluent Python - Chapter 15
Fluent Python - Chapter 15Fluent Python - Chapter 15
Fluent Python - Chapter 15
Sunghyun Lee
 
Conexion php
Conexion phpConexion php
Conexion php
Luis Reategui Vargas
 

What's hot (19)

Drupal.Behaviors
Drupal.BehaviorsDrupal.Behaviors
Drupal.Behaviors
 
J querypractice
J querypracticeJ querypractice
J querypractice
 
Javascript ch8
Javascript ch8Javascript ch8
Javascript ch8
 
Java script
Java scriptJava script
Java script
 
Introducing AngularJS
Introducing AngularJSIntroducing AngularJS
Introducing AngularJS
 
Evented Javascript
Evented JavascriptEvented Javascript
Evented Javascript
 
JQuery
JQueryJQuery
JQuery
 
Grails UI Primer
Grails UI PrimerGrails UI Primer
Grails UI Primer
 
‎Unit tests automation for legacy code‎ at YAPC::NA 2016
‎Unit tests automation for legacy code‎ at YAPC::NA 2016‎Unit tests automation for legacy code‎ at YAPC::NA 2016
‎Unit tests automation for legacy code‎ at YAPC::NA 2016
 
J Query Public
J Query PublicJ Query Public
J Query Public
 
A single language for backend and frontend from AngularJS to cloud with Clau...
A single language for backend and frontend  from AngularJS to cloud with Clau...A single language for backend and frontend  from AngularJS to cloud with Clau...
A single language for backend and frontend from AngularJS to cloud with Clau...
 
So cal0365productivitygroup feb2019
So cal0365productivitygroup feb2019So cal0365productivitygroup feb2019
So cal0365productivitygroup feb2019
 
J query1
J query1J query1
J query1
 
jQuery : Events are where it happens!
jQuery : Events are where it happens!jQuery : Events are where it happens!
jQuery : Events are where it happens!
 
J query
J queryJ query
J query
 
XQDT - XQuery Getting Momentum in Eclipse
XQDT - XQuery Getting Momentum in EclipseXQDT - XQuery Getting Momentum in Eclipse
XQDT - XQuery Getting Momentum in Eclipse
 
Net/http and the http.handler interface
Net/http and the http.handler interfaceNet/http and the http.handler interface
Net/http and the http.handler interface
 
Fluent Python - Chapter 15
Fluent Python - Chapter 15Fluent Python - Chapter 15
Fluent Python - Chapter 15
 
Conexion php
Conexion phpConexion php
Conexion php
 

Similar to Javascript - Beyond-jQuery

Javascript - Beyond-jQuery
Javascript - Beyond-jQueryJavascript - Beyond-jQuery
Writing Maintainable JavaScript
Writing Maintainable JavaScriptWriting Maintainable JavaScript
Writing Maintainable JavaScript
Andrew Dupont
 
¿Cómo de sexy puede hacer Backbone mi código?
¿Cómo de sexy puede hacer Backbone mi código?¿Cómo de sexy puede hacer Backbone mi código?
¿Cómo de sexy puede hacer Backbone mi código?
jaespinmora
 
jQuery introduction
jQuery introductionjQuery introduction
jQuery introduction
Stijn Van Minnebruggen
 
the next web now
the next web nowthe next web now
the next web now
zulin Gu
 
Javascript Frameworks for Joomla
Javascript Frameworks for JoomlaJavascript Frameworks for Joomla
Javascript Frameworks for Joomla
Luke Summerfield
 
Week 4 - jQuery + Ajax
Week 4 - jQuery + AjaxWeek 4 - jQuery + Ajax
Week 4 - jQuery + Ajax
baygross
 
Java script
Java scriptJava script
Java script
Adrian Caetano
 
Jquery- One slide completing all JQuery
Jquery- One slide completing all JQueryJquery- One slide completing all JQuery
Jquery- One slide completing all JQuery
Knoldus Inc.
 
HelloWorld
HelloWorldHelloWorld
HelloWorld
llynn83wou
 
eXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction TrainingeXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction Training
Hoat Le
 
JavaScript: Ajax & DOM Manipulation
JavaScript: Ajax & DOM ManipulationJavaScript: Ajax & DOM Manipulation
JavaScript: Ajax & DOM Manipulation
borkweb
 
jQuery secrets
jQuery secretsjQuery secrets
jQuery secrets
Bastian Feder
 
Flutter DartI have these files in picture I wrote most the codes b.pdf
Flutter DartI have these files in picture I wrote most the codes b.pdfFlutter DartI have these files in picture I wrote most the codes b.pdf
Flutter DartI have these files in picture I wrote most the codes b.pdf
info431285
 
Modern frontend development with VueJs
Modern frontend development with VueJsModern frontend development with VueJs
Modern frontend development with VueJs
Tudor Barbu
 
jQuery secrets
jQuery secretsjQuery secrets
jQuery secrets
Bastian Feder
 
international PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secretsinternational PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secrets
smueller_sandsmedia
 
Building High Performance Web Applications and Sites
Building High Performance Web Applications and SitesBuilding High Performance Web Applications and Sites
Building High Performance Web Applications and Sites
goodfriday
 
Jquery
JqueryJquery
Web 6 | JavaScript DOM
Web 6 | JavaScript DOMWeb 6 | JavaScript DOM
Web 6 | JavaScript DOM
Mohammad Imam Hossain
 

Similar to Javascript - Beyond-jQuery (20)

Javascript - Beyond-jQuery
Javascript - Beyond-jQueryJavascript - Beyond-jQuery
Javascript - Beyond-jQuery
 
Writing Maintainable JavaScript
Writing Maintainable JavaScriptWriting Maintainable JavaScript
Writing Maintainable JavaScript
 
¿Cómo de sexy puede hacer Backbone mi código?
¿Cómo de sexy puede hacer Backbone mi código?¿Cómo de sexy puede hacer Backbone mi código?
¿Cómo de sexy puede hacer Backbone mi código?
 
jQuery introduction
jQuery introductionjQuery introduction
jQuery introduction
 
the next web now
the next web nowthe next web now
the next web now
 
Javascript Frameworks for Joomla
Javascript Frameworks for JoomlaJavascript Frameworks for Joomla
Javascript Frameworks for Joomla
 
Week 4 - jQuery + Ajax
Week 4 - jQuery + AjaxWeek 4 - jQuery + Ajax
Week 4 - jQuery + Ajax
 
Java script
Java scriptJava script
Java script
 
Jquery- One slide completing all JQuery
Jquery- One slide completing all JQueryJquery- One slide completing all JQuery
Jquery- One slide completing all JQuery
 
HelloWorld
HelloWorldHelloWorld
HelloWorld
 
eXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction TrainingeXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction Training
 
JavaScript: Ajax & DOM Manipulation
JavaScript: Ajax & DOM ManipulationJavaScript: Ajax & DOM Manipulation
JavaScript: Ajax & DOM Manipulation
 
jQuery secrets
jQuery secretsjQuery secrets
jQuery secrets
 
Flutter DartI have these files in picture I wrote most the codes b.pdf
Flutter DartI have these files in picture I wrote most the codes b.pdfFlutter DartI have these files in picture I wrote most the codes b.pdf
Flutter DartI have these files in picture I wrote most the codes b.pdf
 
Modern frontend development with VueJs
Modern frontend development with VueJsModern frontend development with VueJs
Modern frontend development with VueJs
 
jQuery secrets
jQuery secretsjQuery secrets
jQuery secrets
 
international PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secretsinternational PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secrets
 
Building High Performance Web Applications and Sites
Building High Performance Web Applications and SitesBuilding High Performance Web Applications and Sites
Building High Performance Web Applications and Sites
 
Jquery
JqueryJquery
Jquery
 
Web 6 | JavaScript DOM
Web 6 | JavaScript DOMWeb 6 | JavaScript DOM
Web 6 | JavaScript DOM
 

Recently uploaded

WMF 2024 - Unlocking the Future of Data Powering Next-Gen AI with Vector Data...
WMF 2024 - Unlocking the Future of Data Powering Next-Gen AI with Vector Data...WMF 2024 - Unlocking the Future of Data Powering Next-Gen AI with Vector Data...
WMF 2024 - Unlocking the Future of Data Powering Next-Gen AI with Vector Data...
Luigi Fugaro
 
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Paul Brebner
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
dakas1
 
Orca: Nocode Graphical Editor for Container Orchestration
Orca: Nocode Graphical Editor for Container OrchestrationOrca: Nocode Graphical Editor for Container Orchestration
Orca: Nocode Graphical Editor for Container Orchestration
Pedro J. Molina
 
The Comprehensive Guide to Validating Audio-Visual Performances.pdf
The Comprehensive Guide to Validating Audio-Visual Performances.pdfThe Comprehensive Guide to Validating Audio-Visual Performances.pdf
The Comprehensive Guide to Validating Audio-Visual Performances.pdf
kalichargn70th171
 
Manyata Tech Park Bangalore_ Infrastructure, Facilities and More
Manyata Tech Park Bangalore_ Infrastructure, Facilities and MoreManyata Tech Park Bangalore_ Infrastructure, Facilities and More
Manyata Tech Park Bangalore_ Infrastructure, Facilities and More
narinav14
 
ppt on the brain chip neuralink.pptx
ppt  on   the brain  chip neuralink.pptxppt  on   the brain  chip neuralink.pptx
ppt on the brain chip neuralink.pptx
Reetu63
 
Upturn India Technologies - Web development company in Nashik
Upturn India Technologies - Web development company in NashikUpturn India Technologies - Web development company in Nashik
Upturn India Technologies - Web development company in Nashik
Upturn India Technologies
 
How GenAI Can Improve Supplier Performance Management.pdf
How GenAI Can Improve Supplier Performance Management.pdfHow GenAI Can Improve Supplier Performance Management.pdf
How GenAI Can Improve Supplier Performance Management.pdf
Zycus
 
Alluxio Webinar | 10x Faster Trino Queries on Your Data Platform
Alluxio Webinar | 10x Faster Trino Queries on Your Data PlatformAlluxio Webinar | 10x Faster Trino Queries on Your Data Platform
Alluxio Webinar | 10x Faster Trino Queries on Your Data Platform
Alluxio, Inc.
 
TMU毕业证书精仿办理
TMU毕业证书精仿办理TMU毕业证书精仿办理
TMU毕业证书精仿办理
aeeva
 
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
The Third Creative Media
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
dakas1
 
Going AOT: Everything you need to know about GraalVM for Java applications
Going AOT: Everything you need to know about GraalVM for Java applicationsGoing AOT: Everything you need to know about GraalVM for Java applications
Going AOT: Everything you need to know about GraalVM for Java applications
Alina Yurenko
 
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdfBaha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid
 
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
kalichargn70th171
 
The Role of DevOps in Digital Transformation.pdf
The Role of DevOps in Digital Transformation.pdfThe Role of DevOps in Digital Transformation.pdf
The Role of DevOps in Digital Transformation.pdf
mohitd6
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
Alina Yurenko
 
The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024
Yara Milbes
 

Recently uploaded (20)

WMF 2024 - Unlocking the Future of Data Powering Next-Gen AI with Vector Data...
WMF 2024 - Unlocking the Future of Data Powering Next-Gen AI with Vector Data...WMF 2024 - Unlocking the Future of Data Powering Next-Gen AI with Vector Data...
WMF 2024 - Unlocking the Future of Data Powering Next-Gen AI with Vector Data...
 
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
 
Orca: Nocode Graphical Editor for Container Orchestration
Orca: Nocode Graphical Editor for Container OrchestrationOrca: Nocode Graphical Editor for Container Orchestration
Orca: Nocode Graphical Editor for Container Orchestration
 
The Comprehensive Guide to Validating Audio-Visual Performances.pdf
The Comprehensive Guide to Validating Audio-Visual Performances.pdfThe Comprehensive Guide to Validating Audio-Visual Performances.pdf
The Comprehensive Guide to Validating Audio-Visual Performances.pdf
 
Manyata Tech Park Bangalore_ Infrastructure, Facilities and More
Manyata Tech Park Bangalore_ Infrastructure, Facilities and MoreManyata Tech Park Bangalore_ Infrastructure, Facilities and More
Manyata Tech Park Bangalore_ Infrastructure, Facilities and More
 
ppt on the brain chip neuralink.pptx
ppt  on   the brain  chip neuralink.pptxppt  on   the brain  chip neuralink.pptx
ppt on the brain chip neuralink.pptx
 
Upturn India Technologies - Web development company in Nashik
Upturn India Technologies - Web development company in NashikUpturn India Technologies - Web development company in Nashik
Upturn India Technologies - Web development company in Nashik
 
How GenAI Can Improve Supplier Performance Management.pdf
How GenAI Can Improve Supplier Performance Management.pdfHow GenAI Can Improve Supplier Performance Management.pdf
How GenAI Can Improve Supplier Performance Management.pdf
 
Alluxio Webinar | 10x Faster Trino Queries on Your Data Platform
Alluxio Webinar | 10x Faster Trino Queries on Your Data PlatformAlluxio Webinar | 10x Faster Trino Queries on Your Data Platform
Alluxio Webinar | 10x Faster Trino Queries on Your Data Platform
 
TMU毕业证书精仿办理
TMU毕业证书精仿办理TMU毕业证书精仿办理
TMU毕业证书精仿办理
 
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
 
Going AOT: Everything you need to know about GraalVM for Java applications
Going AOT: Everything you need to know about GraalVM for Java applicationsGoing AOT: Everything you need to know about GraalVM for Java applications
Going AOT: Everything you need to know about GraalVM for Java applications
 
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdfBaha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
 
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
 
The Role of DevOps in Digital Transformation.pdf
The Role of DevOps in Digital Transformation.pdfThe Role of DevOps in Digital Transformation.pdf
The Role of DevOps in Digital Transformation.pdf
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
 
The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024
 
bgiolcb
bgiolcbbgiolcb
bgiolcb
 

Javascript - Beyond-jQuery

  • 2.
  • 4. Variables // string variable var title = "Content.js Yo!"; // jQuery object var $body = $("body"); // Global variable window.$body = $body; // Initialize an object var contentSettings = { titleEl: 'h1', sectionNameEl: 'h3', color: "blue" }; // Initialize an array var sectionNames = ["Section 1", "Section 2"]; var sectionContent = ["Section 1 content...", "Section 2 content..."];
  • 5. Functions // Standard function function init(title) { jQuery("body").append(title); } // Anonymous function (function(title) { jQuery("body").append(title); })();
  • 6. <script> // string variable var title = "Content.js Yo!"; // jQuery object var $body = $("body"); // Global variable window.$body = $body; // Initialize an object var contentSettings = { titleEl: 'h1', sectionNameEl: 'h3', color: "blue" }; // Initialize an array var sectionNames = ["Section 1", "Section 2"]; var sectionContent = ["Section 1 content...", "Section 2 content..."]; function init(title) { jQuery("body").append(title); }; init(title); </script> Scope All of these variables and the function are all in the global namespace, which could cause conflicts.
  • 7. <script> (function($) { // string variable var title = "Content.js Yo!"; // jQuery object var $body = $("body"); // Global variable window.$body = $body; // Initialize an object var contentSettings = { titleEl: 'h1', sectionNameEl: 'h3', color: "blue" }; // Initialize an array var sectionNames = ["Section 1", "Section 2"]; var sectionContent = ["Section 1 content...", "Section 2 content..."]; wcpdxInit(title); })(jQuery); function wcpdxInit(title) { window.$body.html(title); } </script> Scope So let's wrap our variables in an anonymous function and namespace our function.
  • 8. var $tab = jQuery('.section'); $tab.fadeIn(); $tab.addClass('active'); //////// // vs //////// jQuery('.section').fadeIn(); jQuery('.section').addClass('active'); Caching Caching DOM queries can be a great way to make your code more performant, but be careful!
  • 10. <script> (function($) { // Initialize an object var contentSettings = { titleEl: 'h1', sectionNameEl: 'h3', color: "blue", title: 'Content.js Yo!', sectionNames: ["Section 1", "Section 2"], sectionContent: ["Section 1 content...", "Section 2 content..."] }; wcpdxInit(contentSettings, $); })(jQuery); function wcpdxInit(settings, $) { var $heading = $(document.createElement(settings.titleEl)); var $body = $('body'); var i = 0; // Set the title $body.html($heading.html(settings.title)); // loop through all of our content for(i = -1; i < settings.sectionNames.length; i ++) { // Create our DOM elements var $sectionHeading = $(document.createElement(settings.sectionNameEl)); var $sectionContent = $(document.createElement('p')).css('color', settings.color); $sectionHeading.html(settings.sectionNames[i]); $body.append($sectionHeading); $body.append($sectionContent.html(settings.sectionContent[i])); } } </script> Final
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 18. The Console Use "console.log( ... )" to print things from your script to the browser console.
  • 19. Breakpoints Add a break point to the Source in the browser console, or add "debugger;" to your script.