BCA 4th Sem
BCA 4th Sem
Objects in JavaScript
1. Native Objects – Built-in objects like Array, String, Number, Date, etc.
Creating Objects
1. Object Literal
5. obj.name = "John";
8. this.name = name;
9. this.age = age;
10. }
17. }
18. }
Object Methods
let person = {
name: "John",
greet: function() {
};
Prototype in JavaScript
Every JavaScript object has a hidden [[Prototype]] property that links to another object.
function Person(name) {
this.name = name;
Person.prototype.greet = function() {
};
function Animal(name) {
this.name = name;
Animal.prototype.speak = function() {
};
function Dog(name) {
Animal.call(this, name);
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.bark = function() {
};
let d = new Dog("Buddy");
Modifiers
• g – Global search
• i – Case-insensitive search
• m – Multi-line search
RegExp Patterns
• ^ – Start of a string
• $ – End of a string
• \d – Digits (0-9)
• \s – Whitespace
• * – 0 or more occurrences
• + – 1 or more occurrences
RegExp Methods
• console.log(match[0]); // "hello"
• console.log(str.match(/hello/)); // ["hello"]
Explicit Conversion
• String Conversion
• console.log(String(num)); // "123"
• console.log(num.toString()); // "123"
• Number Conversion
• console.log(Number("123")); // 123
• console.log(parseInt("123px")); // 123
• console.log(parseFloat("12.3px")); // 12.3
• Boolean Conversion
• console.log(Boolean(0)); // false
• console.log(Boolean(1)); // true
• console.log(Boolean("hello")); // true
This provides a solid introduction to JavaScript Objects, Regular Expressions, and Type
Conversion. Let me know if you need further details!
JavaScript events allow you to detect user interactions like clicks, keystrokes, or mouse
movements.
JavaScript Events
Events are triggered by user actions (e.g., clicking a button) or system actions (e.g., page
loading).
Event Handler
function showMessage() {
alert("Button clicked!");
document.getElementById("btn").onclick = showMessage;
Event Flow
Event Listeners
document.getElementById("btn").addEventListener("click", function() {
alert("Button clicked!");
});
Event Types
The DOM represents a web page as a tree structure where elements are nodes.
Types of DOM
document.getElementById("demo").innerHTML = "Hello!";
document.getElementsByClassName("myClass")[0].style.color = "red";
document.querySelector("p").style.fontSize = "20px";
• Changing Content
• Changing Attributes
• document.getElementById("image").src = "new-image.jpg";
• Adding/Removing Elements
• document.body.appendChild(newEl);
Handling Images
document.getElementById("img").src = "new-pic.jpg";
Table Manipulation
box.style.transform = "translateX(100px)";
This provides a strong foundation for event handling and DOM manipulation in JavaScript. Let
me know if you need more details!
The BOM allows JavaScript to interact with the browser (e.g., manipulating the window, history,
location, etc.).
Common Methods
BOM Components
BOM Navigator (Browser Info)
Cookies in JavaScript
Type Description
Persistent Cookies Stored with an expiration date, remain after browser restarts.
Managing Cookies
console.log(name);
Form Validation
function validateForm() {
let x = document.forms["myForm"]["username"].value;
if (x == "") {
return false;
Validation APIs
This provides an overview of BOM, cookies, and form handling in JavaScript. Let me know if you
need further explanations!
Introduction to jQuery
jQuery is a fast, lightweight JavaScript library that simplifies HTML DOM manipulation, event
handling, animations, and AJAX interactions.
jQuery Syntax
Basic syntax:
$(selector).action();
Example:
$(document).ready(function() {
$("p").css("color", "blue");
});
or
$(function() {
$("p").css("color", "blue");
});
jQuery Selectors
jQuery Events
$("#btn").click(function() {
alert("Button clicked!");
});
Common events:
• click()
• dblclick()
• hover()
• keydown(), keyup()
• focus(), blur()
jQuery Effects
Predefined animations.
Custom animations:
jQuery Traversing
jQuery AJAX
$.get("data.txt", function(data) {
$("#content").text(data);
});
$.ajax({
url: "data.json",
method: "GET",
success: function(response) {
console.log(response);
});
jQuery Miscellaneous
• $("li").each(function() {
• console.log($(this).text());
• });
• $("#box").addClass("highlight");
• $("#box").removeClass("highlight");
• $jq("p").hide();
This gives a quick overview of jQuery. Let me know if you need more details!