Class notes 2 (1)
Class notes 2 (1)
1. Introduction to JavaScript
JavaScript is a dynamic programming language used for web development, enabling
interactivity on web pages. It is a loosely typed language and supports both object-oriented
and functional programming paradigms.
2. Objects in JavaScript
Objects are key-value pairs that store data and functions (methods).
a. Object Creation
b. Object Methods
let student = {
name: "John",
getName: function() {
return this.name;
}
};
console.log(student.getName()); // Output: John
a. call()
function introduce(city) {
console.log(`Hello, I am ${this.name} from ${city}`);
}
b. apply()
c. bind()
4. Functions in JavaScript
Functions are reusable blocks of code.
a. Function Syntax
function greet(name) {
return "Hello " + name;
}
console.log(greet("John")); // Output: Hello John
b. Function Hoisting
c. Arrow Functions
d. Nested Functions
function outer() {
function inner() {
console.log("Inside inner function");
}
inner();
}
outer();
(function() {
console.log("I am an IIFE!");
})(); // Output: I am an IIFE!
f. Callback Functions
function done() {
console.log("Task Completed");
}
greetUser("Alice", done);
5. Classes in JavaScript
Classes provide an OOP approach.
a. Inheritance
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(`${this.name} makes a sound`);
}
}
b. Shadowing Method
A method in a child class with the same name as in the parent class.
d. Static Methods
class MathUtil {
static add(a, b) {
return a + b;
}
}
console.log(MathUtil.add(5, 10)); // Output: 15
6. Promises
Used for asynchronous operations.
a. Creating a Promise
let promise = new Promise((resolve, reject) => {
let success = true;
if (success) resolve("Task completed");
else reject("Task failed");
});
c. Promise.all()
let p1 = Promise.resolve("First");
let p2 = Promise.resolve("Second");
let p3 = Promise.resolve("Third");
d. Promise.any()
let p4 = Promise.reject("Failed");
let p5 = Promise.resolve("Success");
Promise.any([p4, p5]).then(console.log); // Output: Success
e. Chaining Promises
new Promise((resolve) => resolve(2))
.then((num) => num * 2)
.then((num) => num * 3)
.then(console.log); // Output: 12
f. async/await
1. Introduction to jQuery
jQuery is a lightweight, fast, and feature-rich JavaScript library that simplifies HTML document
traversal, event handling, animations, and AJAX interactions.
html
CopyEdit
<!DOCTYPE html>
<html lang="en">
<head>
<title>jQuery Example</title>
<script
src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<button id="btn">Click Me</button>
<p id="text">Hello, jQuery!</p>
<script>
$(document).ready(function(){
$("#btn").click(function(){
$("#text").text("Button Clicked!");
});
});
</script>
</body>
</html>
📝 Explanation:
● $() selects elements.
● .ready() ensures the script runs only after the document loads.
● .click() adds an event listener.
2. jQuery Effects
jQuery provides built-in effects like hide, show, fade, slide, and toggle.
$("#showBtn").click(function(){
$("#box").show(); // Shows the element
});
b. Toggle
javascript
CopyEdit
$("#toggleBtn").click(function(){
$("#box").toggle(); // Toggles visibility
});
c. Fade Effects
javascript
CopyEdit
$("#fadeIn").click(function(){
$("#box").fadeIn(); // Fades in the element
});
$("#fadeOut").click(function(){
$("#box").fadeOut(); // Fades out the element
});
d. Slide Effects
javascript
CopyEdit
$("#slideUp").click(function(){
$("#box").slideUp(); // Slides up
});
$("#slideDown").click(function(){
$("#box").slideDown(); // Slides down
});
3. jQuery Callbacks
A callback function executes after a function completes execution.
javascript
CopyEdit
$("#fadeBtn").click(function(){
$("#box").fadeOut(1000, function(){
alert("Fade out completed!");
});
});
javascript
CopyEdit
$("#chainBtn").click(function(){
$("#box").css("background-color",
"blue").slideUp(1000).slideDown(1000);
});
📝 Explanation: The element's background changes to blue, slides up, and then slides down.
5. jQuery noConflict()
If multiple libraries use $, noConflict() prevents conflicts.
html
CopyEdit
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
var jq = jQuery.noConflict();
jq(document).ready(function(){
jq("#btn").click(function(){
jq("#text").text("No Conflict Mode!");
});
});
</script>
📝 Explanation:
● $ is replaced with jq to avoid conflicts with other libraries.
6. jQuery Animation
The .animate() function creates custom animations.
javascript
CopyEdit
$("#animateBtn").click(function(){
$("#box").animate({left: "200px", opacity: "0.5"}, 1000);
});
📝 Explanation: Moves the element 200px to the right and reduces opacity over 1 second.
7. jQuery Traversing
Traversing allows navigating the DOM tree to find elements.
$("#childBtn").click(function(){
$("#parent").children().css("color", "blue");
});
b. Siblings
$("#siblingBtn").click(function(){
$("#child").siblings().css("background", "yellow");
});
c. Closest & Find
$("#closestBtn").click(function(){
$("#child").closest("div").css("border", "2px solid green");
});
$("#findBtn").click(function(){
$("#parent").find("p").css("font-weight", "bold");
});
Tab 2
🔹 JavaScript Concepts
1️⃣ Introduction to JavaScript
● JavaScript is a client-side scripting language used for dynamic and interactive web
pages.
● It is an interpreted, event-driven, and loosely typed language.
● Can be used for both front-end (DOM manipulation) and back-end (Node.js)
development.
a) Object Creation
b) Object Methods
a) Syntax
js
CopyEdit
function add(a, b) {
return a + b;
}
b) Function Hoisting
c) Arrow Functions
js
CopyEdit
const sum = (a, b) => a + b;
d) Nested Functions
f) Callback Functions
js
CopyEdit
function greet(name, callback) {
console.log("Hello, " + name);
callback();
}
a) Inheritance
js
CopyEdit
class Parent {
constructor(name) {
this.name = name;
}
}
class Child extends Parent {
constructor(name, age) {
super(name);
this.age = age;
}
}
b) Shadowing Method
c) Private Methods
js
CopyEdit
class Person {
#privateMethod() {
console.log("Private method");
}
}
d) Static Methods
js
CopyEdit
class MathUtil {
static add(a, b) {
return a + b;
}
}
a) Promise Creation
js
CopyEdit
let myPromise = new Promise((resolve, reject) => {
let success = true;
success ? resolve("Success!") : reject("Error!");
});
b) Error Handling
e) Async Await
js
CopyEdit
async function fetchData() {
let data = await fetch("https://api.example.com");
return data.json();
}
🔹 jQuery Concepts
1️⃣ Introduction to jQuery
js
CopyEdit
$("#btn").click(function() {
$("#box").fadeOut();
});
js
CopyEdit
$("#box").fadeOut(500, function() {
alert("Animation Done");
});
📌 University Exam Question
💡 What is a jQuery callback function?
✅ Solution:
1. Function executed after another function.
2. Ensures code runs in sequence.
js
CopyEdit
$("#box").slideUp().fadeIn();
5️⃣ noConflict()
js
CopyEdit
var jq = $.noConflict();
jq("#box").hide();
js
CopyEdit
$("#box").animate({width: "300px"});
js
CopyEdit
$("ul li:first").css("color", "red");