Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Monday, September 30, 2019

JavaScript Array map() Method


The method map() of array always creates a new array without modify the original array and the map method calls the provided function or execute the statement for each array elements

You can use map () method to iterate of each element of array instead of using JavaScript looping function like for or foreach method and it will reduce number of lines code to compare for or foreach method

Here is an example of map method to calculate the square value for each element of array

const sqrs = [1,2,3].map(x => x * x);

console.log(sqrs);

Output: Array [1, 4, 9]

another example of map method to calculate the square root of each element in the array

const sqrs = [4, 9, 16, 25].map(Math.sqrt);

console.log(sqrs);

Output: Array [2, 3, 4, 5]



Wednesday, September 18, 2019

JavaScript: Reload or refresh the current URL


This blog will discuss how to refresh or reload the current window document or current page in JavaScript by using calling Location.Reload() method

Basically location object contains the information about the current URL and we can easily access location object through the window.location property.

Here is an JavaScript example to reload the current URL by using Location.Reload() method.

         $.ajax({
                url: this.action,
                type: this.method,
                data: $(this).serialize(),
                success: function (result) {
                    if (result == "Record was saved successfully") { 
                        location.reload();
                    }
                    else {
                        $('#error).html(result);
                    }
                }

In above example, we are posting form by using Ajax Post, on success event, if record was save successfully, we are reloading the current URL.

There is optional parameter for Reload () method, we can specify the optional parameter value to true to enforce to reload () method to reload current page from server instead from cache.

location.reload(true);


Thursday, September 5, 2019

Prototype in JavaScript


In this post, we will discuss what are prototypes in JavaScript, how they help JavaScript in achieving the concepts of Object Oriented Programming.

JavaScript is a very dynamic programming and it allows to add new properties or function on object at any time.

Adding new property by using Prototype

Here is an example of adding new property by using Prototype

<script type="text/javascript">
    class Person {
        constructor(firstName, lastName) {
            this.firstName = firstName;
            this.lastName = lastName;         
        }

        name() {
            return this.firstName + ' ' + this.lastName;
        }
    };

   _person = new Person("Henry", "Ford");
   _person.Age = 18;

    console.log(_person.name());
    console.log(_person.Age);
</script>

Output :

Henry Ford
18

By using Prototype, we can define new property Age of Person object.

Adding new function by using Prototype:

Here is an example of adding new function by using Prototype

<script type="text/javascript">
    class Person {
        constructor(firstName, lastName) {
            this.firstName = firstName;
            this.lastName = lastName;         
        }

        name() {
            return this.firstName + ' ' + this.lastName;
        }
    };


   _person = new Person("Henry", "Ford");
   _person.hello = function () { return "Hi I am " + this.firstName + ' ' + this.lastName; }
  
    console.log(_person.hello());

</script>

Output:

Hi I am Henry Ford

By using Prototype, we can define new function ‘hello’ method of Person object.


Thursday, August 29, 2019

JavaScript Class : The super and extends keywords

In blog, we learn about JavaScript class and how to implement inheritance by using supper and extends keyword

The keyword extends is used to implement the inheritance in JavaScript class and we can create a subclass or child class by using the ‘extends’ keyword. In inheritance, the child class is derived with base class and has own extra business logic.


Here is an example which demonstrates how to inherit Person Class and create an Employee Class.

  class Person {
        constructor(firstName, lastName) {
            this.firstName = firstName;
            this.lastName = lastName;         
        }

        name() {
            return this.firstName + ' ' + this.lastName;
        }
    };

    class Employee extends Person {
        constructor(firstName, lastName, employeeID, department) {        
            super(firstName, lastName);
            this.employeeID = employeeID;
            this.department = department;
        }     
    };

The keyword ‘super’ is used to call the constructor method of base class. The child class has always a constructor method, it should call to super() method to invoke base class constructor method and initialize the properties of base class.

    var _person = new Employee("Henry", "Ford", 100, "IT");

    console.log(_person.name()); 
    console.log(_person.department);

Console message:

Henry Ford
IT

Introduction to JavaScript class


A JavaScript class is a type of function. Classes are declared with the class keyword, and always add a constructor method and this constructor method is called each time when the class object is initialized

Defining a Class:

We use keyword Class to create a class.

Here is an example which demonstrates how to define the class “Person”

  class Person {
        constructor(firstName, lastName) {
            this.firstName = firstName;
            this.lastName = lastName;         
        }
    }


As we know, the constructor method always be called, when the class Person will be initialized.

We will create an instance of Person Class by using the new keyword, and can pass values through constructor.

 var _person = new Person("Henry", "Ford");

Here is a complete example (HTML & JS)

<h2>JavaScript Class - Demo</h2>

<p id="personDiv"></p>

<script>
    class Person {
        constructor(firstName, lastName) {
            this.firstName = firstName;
            this.lastName = lastName;         
        }
    };

    var _person = new Person("Henry", "Ford");
    document.getElementById("personDiv").innerHTML = _person.firstName;

</script>

Defining a Method:

The constructor of class is a default of method, it is used to initialize the class properties. You can define own custom method.

Here is an example which demonstrates how to define method name() in Person class to get person’s full name

    class Person {
        constructor(firstName, lastName) {
            this.firstName = firstName;
            this.lastName = lastName;         
        }

        name() {
            return this.firstName + ' ' + this.lastName;
        }
    };


Inheritance - Extending a Class:

We use keyword extends to implement class inheritance and derived class inherits all properties and method of base class.

We use keyword super to call the constructor method of base class or parent class.

Here is an example which demonstrates how to inherit Person Class and create an Employee Class.

https://d.adroll.com/cm/aol/out?adroll_fpc=dadc06851b6c20ac88ab6464b1659917-1567086153020&xid_ch=f&advertisable=S4BPDI4QWNB57PEKEZSLIPclass Employee extends Person {
        constructor(firstName, lastName, employeeID, department) {        
            super(firstName, lastName);
            this.employeeID = employeeID;
            this.department = department;
        }     
    };
Here is a complete example (HTML & JS)

<h2>JavaScript Class - Demo</h2>

<p id="personDiv"></p>
<p id="personDepartment"></p>
<p id="personEmpID"></p>

<script>
    class Person {
        constructor(firstName, lastName) {
            this.firstName = firstName;
            this.lastName = lastName;         
        }

        name() {
            return this.firstName + ' ' + this.lastName;
        }
    };

    class Employee extends Person {
        constructor(firstName, lastName, employeeID, department) {        
            super(firstName, lastName);
            this.employeeID = employeeID;
            this.department = department;
        }     
    };
   

    _person = new Employee("Henry", "Ford", 100, "IT");

    document.getElementById("personDiv").innerHTML = _person.name();
    document.getElementById("personDepartment").innerHTML = _person.department;
    document.getElementById("personEmpID").innerHTML = _person.employeeID;
</script>

Thursday, April 18, 2019

JavaScript Equal Operator == vs ===

Javascript has two equality comparison operator - double equals “==” and triple equals “===”.  Both are used to compare equality of two values and most of time both operator returns same result, until both value are not same data type.

Let us first discuss double equals “==” aka loose equality, in loose equality, first it converts into command type and then it compares equality. 
Example : 

var a = 10,
var b = 10
a == b 
// true

Javascript equality table

   Source : https://dorey.github.io/JavaScript-Equality-Table/

Triple equal “===” aka strictly equality, in strict equality, it does not perform any data type conversion, if both values are not same type, it returns false value. 
Example :

var a = 10,
var b = 10
a=== b 
// false

Javascript equality table

Source : https://dorey.github.io/JavaScript-Equality-Table/

In both example, double equal “==”, it first converts the string ‘10’ and number 10 in same common type and then it compares, so it returns True. But in triple equal “===”, it founds both value are different type and it simply returns False

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...