Web Programming Assignment Guide
Web Programming Assignment Guide
Asynchronous programming in JavaScript allows non-blocking execution, enabling programs to run multiple tasks simultaneously, improving performance. Callbacks involve passing a function as an argument to another function to execute later, which can lead to callback hell due to nested structures. Promises offer better readability by allowing chaining with .then() and .catch(). An example is: ```javascript fetch('api/data').then(response => response.json()).then(data => console.log(data)); ``` Async/await, built on promises, allows writing asynchronous code as if it were synchronous, improving clarity. Example: ```javascript async function fetchData() { const response = await fetch('api/data'); const data = await response.json(); console.log(data); } ``` Async/await provides cleaner and more manageable code compared to callbacks and promises .
Event handling in JavaScript is the process of responding to events triggered by actions like mouse clicks, keyboard inputs, or window resizing. The addEventListener method attaches an event handler to a specific element without overwriting existing events. An example of handling a click event is: ```javascript const button = document.getElementById('myButton'); button.addEventListener('click', function() { alert('Button clicked!'); }); ``` Here, clicking the button with id='myButton' shows an alert box .
In JavaScript, var, let, and const are used to declare variables but have different scopes and properties. 'var' is function-scoped and can lead to issues like hoisting, where a variable can be referenced before its declaration. 'let' is block-scoped, meaning its visibility is limited to the block it is defined in, thus preventing hoisting problems. 'const' is also block-scoped but is used for variables whose values should not change after initialization. The differences are significant in situations involving loops or conditional blocks, where 'let' and 'const' prevent accidental global declarations that can happen with 'var'. Additionally, using 'const' ensures constancy in variables that should not change .
Sessions and cookies in PHP are mechanisms to manage user data across requests. Sessions store data on the server side, preserving state across pages via a unique session id. Example: ```php session_start(); $_SESSION['username'] = 'JohnDoe'; echo $_SESSION['username']; ``` Cookies store data on the client-side, sent with each request. Example: ```php setcookie('username', 'JohnDoe', time() + 3600); echo $_COOKIE['username']; ``` Sessions provide more security as data isn't exposed to client access, unlike cookies, which can be manipulated by users .
Inheritance in Python's OOP allows a class (derived) to inherit properties and methods from another class (base), facilitating code reuse and logical hierarchy. Example: ```python class Animal: def __init__(self, species): self.species = species def make_sound(self): return 'Sound' class Dog(Animal): def make_sound(self): return 'Bark' my_dog = Dog('Canine') print(my_dog.species) # Output: Canine print(my_dog.make_sound()) # Output: Bark ``` Here, 'Dog' inherits from 'Animal', overriding the make_sound method, illustrating single inheritance .
Block-level elements in HTML are those that occupy the entire width of their parent container and start on a new line. Examples include <div>, <h1> through <h6>, <p>, and <form>. These elements are typically used to structure the main parts of a webpage, like sections and articles. In contrast, inline elements do not start on a new line and only occupy as much width as necessary. Examples include <span>, <a>, and <img>. Inline elements generally contain other content within block-level elements, such as links or formatting text within a paragraph .
There are four main types of CSS positioning: static, relative, absolute, and fixed. Static positioning is the default positioning; elements follow the normal flow of the document. Relative positioning positions elements relative to their original position without affecting other elements. For example, position: relative; top: 10px; moves an element 10 pixels down. Absolute positioning removes the element from the document flow and positions it relative to its closest positioned ancestor, or the initial containing block if there's no ancestor. For instance, position: absolute; top: 20px; positions an element 20 pixels from the top of its ancestor. Fixed positioning positions an element relative to the viewport, unaffected by scrolling. An example is position: fixed; bottom: 10px; which keeps the element 10 pixels from the bottom of the viewport .
CSS selectors are patterns used to select the elements to style in a web page. Class selectors are defined with a preceding dot (.) and can be used on multiple elements. For example, .myClass applies styles to all elements with class="myClass". ID selectors use a preceding hash (#) and apply to a unique element with that specific ID; for example, #myId applies styles to the element with id="myId". Descendant selectors apply styles to elements nested within a specified hierarchy, such as div p, which would apply styles to <p> elements that are descendants of <div> elements .
GET and POST are methods used in PHP to submit form data. GET appends form data to the URL, making it visible and suitable for non-sensitive information. It's accessed via $_GET superglobal and limits data length. POST submits data within the request body, suitable for sensitive or large data, and uses $_POST superglobal. Example of GET: ```html <form method="GET" action="process.php"> <input type="text" name="name" /> <input type="submit" value="Submit" /> </form> ``` Example of POST: ```html <form method="POST" action="process.php"> <input type="text" name="name" /> <input type="submit" value="Submit" /> </form> ``` POST provides enhanced security over GET by not displaying data in the URL .
Decorators in Python are a design pattern that allows the modification of functions or methods. They enable code reuse by wrapping a function to extend or modify its behavior without altering its structure. A decorator is applied with the '@decorator_function' syntax. Example: ```python def my_decorator(func): def wrapper(): print('Before function execution') func() print('After function execution') return wrapper @my_decorator def say_hello(): print('Hello!') say_hello() ``` Here, 'my_decorator' modifies 'say_hello' by adding prints around its execution .