Tuesday, March 3, 2020

JavaScript - Object


Creating a JavaScript Object
var Employee = {firstName:"John",
                lastName:"Doe",
                age:50,
                eyeColor:"Black",
                salary:"60000"};

var Employee = {
  firstName: 
"John",
  lastName: 
"Doe",
  age: 
50,
  eyeColor: 
"blue",
  salary:"60000"
};

var Employee = new Object();
Employee.firstName = 
"John";
Employee.lastName = 
"Doe";
Employee.age = 
50;
Employee.eyeColor = 
"Black";
Employee.salary = "60000";

JavaScript Objects are Mutable

var person = {firstName:"John",
              lastName:"Doe",
              age:50,
              eyeColor:"blue",
              salary:"60000"}

var x = person;
x.age = 
10;           // This will change both x.age and person.age

Javascript - Prototype


<!DOCTYPE html>
<html>
<head>

</head>
<body>

    <script>

        // function constructor
        function Employee(name, job, yearOfBirth) {
            this.name = name;
            this.job = job;
            this.yearOfBirth = yearOfBirth;
        }
        // adding calculateAge() method to the Prototype property
        Employee.prototype.calculateAge = function () {
            console.log('The current age is: ' + (2019 - this.yearOfBirth));
        }
        console.log(Employee.prototype);

        // creating Object Person1
        let Employee1 = new Employee('Elon', 'Astronaut', 1986);
        console.log(Person1)
        let Employee2 = new Employee('ET', 'Alien', 1997);
        console.log(Person2)

        Employee1.calculateAge();
        Employee2.calculateAge();

    </script>

</body>
</html>

Friday, December 6, 2019

Java 0001 : CompletableFuture In Java


We can use CompletableFuture for asynchronous programming which in a non-blocking code. Other than the running main thread, it is used to run a task on a separate thread. Paralleling threads running will avoid the main thread to block or wait the separate thread for task performing until completing. Parallelism thoroughly improve performance of a application.

The most frequently used CompletableFuture methods are:
1.       supplyAsync(): It complete its job asynchronously. The result of supplier is run by a task from ForkJoinPool.commonPool() as default. The supplyAsync() method returns CompletableFuture on which we can apply other methods.
2.       thenApply(): The method accepts function as an arguments. It returns a new CompletableStage when this stage completes normally. The new stage use as the argument to the supplied function.
3.       join(): the method returns the result value when complete. It also throws a CompletionException (unchecked exception) if completed exceptionally.

Example 



JavaScript - Object

Creating a JavaScript Object var  Employee = {firstName: "John" ,                 lastName: "Doe" ,             ...