Functions/ Methods and Classes
in Web Technology
Method/Function:
In JavaScript, functions and methods are fundamental building blocks for creating reusable
code.
Syntax in javascript:
function name(params) {
Example: Function for the calculation of Area of Square
function Area(){
let Length=10;
let Width=10;
const Result = Length*Width;
console.log('Area of Square:' +Result)
}
Classes:
In JavaScript, classes are a blueprint for creating objects. They encapsulate data (properties)
and behavior (methods) into a single unit. Classes provide a structured way to organize and
reuse code, making it easier to create complex applications.
Syntax:
class name {
constructor(parameters) {
}
}
Example: Class of a student
Class will have constructor
It will have some attributes
It will have some methods/functions: functions within class should be written without
function keyword.
class Student {
constructor(Id, Roll_no, Class, Section) {
this.Id=Id;
this.Roll_no=Roll_no
this.Class=Class
this.Section=Section
}
examMarks() {
let Eng=100;
let Urdu=100;
const total=Eng+Urdu;
console.log('Total Marks:'+total)
}
}