Class Function And Event In JavaScript

In this article we will try to learn about class Function and Event In Javascript.
 
As we know all that the above is required in every signle page requirement to fill the data in input control, write the logic accoriding to business requirement and save in data base.
 
So here we will cover the following things,
  • JavaScript Class
  • JavaScript Class Inheritance
  • JavaScript Static Methods
  • JavaScript Function
  • JavaScript Arrow Function
  • Difference between Call() & Apply() function
  • JavaScript Events
  • Summary

JavaScript Class

 
When Javascript was first developed there there were no concept about classes, but ECMAScript2015, that is also known as ES6, introduced JavaScript Classes.
 
Definition: JavaScript classes are blueprints/templates for JavaScript Objects. We can create the object by using new keyword.
 
Eg,
  1. class employee {  
  2.    constructor(name, age) {  
  3.       this.name = name;  
  4.       this.age = age;  
  5.    }  
  6. }  
Here in this example we have created a class with name employee with 2 properties, name & age. We can create any number of properties.
 
Create object,
  1. let emp1 = new employee("Kim", 20);  
  2. let emp2 = new employee("Sara", 21);  
In this object creation we have created 2 objects. You can create any number of objects of the same class based on your business need.
 
Create Method
 
As we have seen that we can create object of any class, other than that we can create method as well.
 
Eg:
  1. class employee {  
  2.    constructor(name, age) {  
  3.       this.name = name;  
  4.       this.age = age;  
  5.    }  
  6.    age() {  
  7.       let date = new Date();  
  8.       return 50 - this.age;  
  9.    }  
  10. }  
Create paramter Method
 
We have created the above method without parameters, but we can also create with parameter method.
 
Eg,
  1. class employee {  
  2.    constructor(name, age) {  
  3.       this.name = name;  
  4.       this.age = age;  
  5.    }  
  6.    age(x) {  
  7.       let date = new Date();  
  8.       return x - this.age;  
  9.    }  
  10. }  
  1. let emp1 = new employee("Kim", 20);  
  2. emp1.age =emp1.age(50);  
Note
The code that we will write in class must be followed in "strict mode", otherwise we will get an error if you do not follow the "strict mode" rules.
 

JavaScript Class Inheritance

 
If we know any object oriented programming language then it's easy to understand about inheritance. It is used to create a class inheritance, and uses the extends keyword. The class which is created with a class inheritance (extend) will inherit all the methods and properties from another class.
 
Eg,
 
Let's create a class name "student" which we will use to inherit the methods-properties from the "teacher" class,
  1. class student {  
  2.    constructor(name) {  
  3.       this.name = name;  
  4.    }  
  5.    present() {  
  6.       return 'My Name is: ' + this.name;  
  7.    }  
  8. }  
  9.   
  10. class teacher extends student {  
  11.    constructor(name, std) {  
  12.       super(name);  
  13.       this.std = std;  
  14.    }  
  15.    show() {  
  16.       return this.present() + ', it is a ' + this.sts;  
  17.    }  
  18. }  
Note
The super() method refers to the parent class (employee). The concept of inheritance is used for a code reusability point of view, which reuses the properties and methods of an existing class when you create a new class.
 

JavaScript Static Methods 

 
The concept of static method is the same as the OOPs concept.
  1. class employee {  
  2.    constructor(name) {  
  3.       this.name = name;  
  4.    }  
  5.    static m1() {  
  6.       return "Hello employee";  
  7.    }  
  8. }  
  9.   
  10. let emp = new employee("Sara");  
  11.   
  12. document.getElementById("p1").innerHTML = employee.m1();  
Note
p1 is the id like it: <p id="demo"></p>
 

JavaScript Function

 
Function is same as any method which in JavaScript functions are defined with the function keyword. The syntaxt of function would be like this,
 
function functionName(param) {
// ToDo code
}
 
eg,
  1. function myFunction(a, b) {  
  2.    return a * b;  
  3. }  
Here in this function it will return the multiple of a and b.
 
Note
A JavaScript function can also be defined using an expression. so that the function expression we can be stored in a variable:
 
eg,
 
var x = function (a, b) {return a * b};
var z = x(4, 3);
 
Self-Invoking Functions
 
A self-invoking function is invoked/called automatically, without being called. This is useful in case of page loading which initially is called if required. If function will need to execute automatically then the expression is followed by ().
 
eg,
  1. <!DOCTYPE html>    
  2. <html>    
  3.    <body>    
  4.     
  5.       <p>Invoked automatically</p>    
  6.     
  7.       <p id="demo"></p>    
  8.     
  9.       <script>    
  10.          (function () {    
  11.             document.getElementById("demo").innerHTML = "hi test function invoked automatically";    
  12.          })();    
  13.       </script>    
  14.     
  15.    </body>    
  16. </html>     

JavaScript Arrow Function

 
Arrow functions isn't a  new concept. It allows a short syntax for writing function expressions. You don't need the function keyword, the return keyword, and the curly brackets.
 
eg,
  1. // ES5  
  2. var x = function(x, y) {  
  3.    return x * y;  
  4. }  
  5.   
  6. // ES6  
  7. const x = (x, y) => x * y;  
Note
Arrow functions are not supported in IE11 or earlier.
 
Function Parameters
 
In normal OOPs concept parameter is checked at the time of passing, but in JavaScript function does not perform any checking on parameter values (arguments). In JavaScript, programming language function definitions do not specify data types for parameters. In JavaScript programming language functions do not check how many arguments it's going to receive.
 
eg,
  1. <!DOCTYPE html>    
  2. <html>    
  3.    <body>    
  4.     
  5.       <p> function parameter.</p>    
  6.       <p id="demo"></p>    
  7.     
  8.       <script>    
  9.          function myFunction(x, y) {    
  10.             if (y === undefined) {    
  11.             y = 4;    
  12.             }    
  13.             return x * y;    
  14.             }    
  15.          document.getElementById("demo").innerHTML = myFunction(4);    
  16.       </script>    
  17.     
  18.    </body>    
  19. </html>    
  20.     
  21. // output: 16     
Default Parameters
 
ECMAScript 2015 or ES6 allows default parameter values in the function declaration,
 
function (x, y = 2) {
// todo code
}
 

Difference between Call() & Apply() function

 
The call() method takes arguments separately. The apply() method takes arguments as an array. The apply() method accepts arguments in an array,
 
eg,
  1. <!DOCTYPE html>  
  2. <html>  
  3. <body>  
  4. <p id="demo"></p>  
  5.   
  6.    <script>  
  7.       var employee = {  
  8.          fullName: function(age, adrs) {  
  9.             return this.firstName + " " + this.lastName + "," + age + "," + adrs;  
  10.          }  
  11.       }  
  12.       var person1 = {  
  13.          firstName:"sara",  
  14.          lastName: "haya"  
  15.       }  
  16.       var x = person.fullName.apply(person1, [21, "dd"]);  
  17.       document.getElementById("demo").innerHTML = x;  
  18.    </script>  
  19.   
  20. </body>  
  21. </html>  
The call() method is a predefined JavaScript method.
 
eg,
  1. <!DOCTYPE html>    
  2. <html>    
  3. <body>    
  4. <p id="demo"></p>    
  5.    <script>    
  6.       var person = {  
  7.           fullName: function() {  
  8.               return this.firstName + " " + this.lastName;  
  9.           }  
  10.       }  
  11.       var person1 = {  
  12.           firstName: "John",  
  13.           lastName: "Doe"  
  14.       }  
  15.       var person2 = {  
  16.           firstName: "Mary",  
  17.           lastName: "Doe"  
  18.       }  
  19.       var x = person.fullName.call(person1);  
  20.       document.getElementById("demo").innerHTML = x;  
  21.    </script>    
  22. </body>    
  23. </html>    
Note
The call() method can accept arguments,
  1. <!DOCTYPE html>    
  2. <html>    
  3. <body>    
  4. <p id="demo"></p>      
  5.    <script>   
  6.       var person = {  
  7.           fullName: function(city, country) {  
  8.               return this.firstName + " " + this.lastName + "," + city + "," + country;  
  9.           }  
  10.       }  
  11.    var person1 = {  
  12.           firstName: "John",  
  13.           lastName: "Doe"  
  14.       }  
  15.       var x = person.fullName.call(person1, "Oslo""Norway");  
  16.       document.getElementById("demo").innerHTML = x;  
  17.    </script>      
  18. </body>    
  19. </html>   

JavaScript Events

 
In JavaScript event can be something that happens on the browser, or something a user does on button click or any action perform. Events that can be a button click, input change, keyup, keydown etc... Which we can do on single quotes or double quotes like this,
 
<element event='some JavaScript'>
<element event="some JavaScript">
 
eg,
 
<button onclick="document.getElementById('demo').innerHTML = Date()">The time is?</button>
or
<button onclick="this.innerHTML = Date()">The time is?</button>
 
Note
As we know that javaScript code is often many lines long. 
  1. <!DOCTYPE html>    
  2. <html>    
  3. <body>    
  4. <button onclick="displayDate()">The time is?</button>    
  5.    <script>    
  6.       function displayDate() {    
  7.          document.getElementById("demo").innerHTML = Date();    
  8.       }    
  9.    </script>    
  10.   <p id="demo"></p>    
  11. </body>    
  12. </html>   

Common HTML Events

 
Here is a list of some common HTML events,
 
 

Summary

 
Congratulations on following the steps. I hope you liked this tutorial and please share it with others. Thanks for taking your valuable time to read the full article.


Similar Articles