Lab 02
<html>
<head>
</head>
<body>
<script type='text/javascript'>
//01.) Promises/Asynchronous and callbacks /////////////////////
//after 2s this is executing
setTimeout(function (){
console.log("Executed after 2s");
},2000);
console.log("Hello"); //this is executing before first command
*/
//////////////////////////////
let printME = function(value){
console.log(value);
};
let getValue = function(callback) {
let value =0;
setTimeout(function(){
value=10;
callback(value); //by this callback function you will execute the print me function
return value;
},2000);
};
console.log(getValue(printME)); //returning undefined & 10
//////////////////////////////
let getValue = function(callback){
let value=0;
return new Promise(function(resolve,reject){
getTimeOut(function(){
value=10;
resolve(value);
},2000); //without any call this will work because of promise
});
};
getValue().then(function(value){
console.log(value);
});
console.log("i trusted you !!"); //output i trused you!!! 10
//02). Classes in JavaScript /////////////////////////////////
function vehicle(type){ //create the class
this.type = type;
Vehicle.vehicleCount++;
}
Vehicle.prototype.drive = function(){ //method
console.log("Vehicle is driving");
};
Vehicle.vehicleCount = 0 ; //intializing the vehicle count to zero ;;;constant static variable
const vehicle =new Vehicle("Toyota"); //creating an object of Vehicle
console.log(vehicle.vehicleCount);
vehicle.print(); //calling print method
function car(type){
Vehicle.call(this.type);
}
Car.prototype = Object.create(Vehicle.prototype);
Car.prototype.constructor =Car;
Car.prototype.balanceWheels =function (){
console.log("Wheels are balanced");
}
const car= new car("Honda");
vehicle.drive();
vehicle.balanceWheels();
car.drive();
car.balanceWheels();
console.log(Vehicle.vehicleCount);
//4. Try exercise 1 with async/await ///////////////////////////////////
function getMsg(){
return new Promise(function(resolve,reject){
setTimeout(function() {
value = "Hello";
resolve(value);
},2000);
});
}
async function print(){
let value = await getMsg();
console.log(value);
}
print();
//05.)Try exercise 2 class, extends, get, set, and super keywords //////////////
class Vehicle{
static vehicount = 0;
constructor(type){
this.type = type;
Vehicle.vehicount++;
}
drive(){
console.log(this.type + " is driving ");
}
}
let vehi = new Vehicle("Toyota");
vehi.drive();
console.log("vehicle count is " +Vehicle.vehicount); //Toyota is driving
//1
//car class
class Car extends Vehicle{
constructor(type){
super(type);
}
wheelsbalanced(){
console.log(this.type + " wheels are balanced......");
}
}
let mycar = new Car("Nissan");
mycar.wheelsbalanced();
mycar.drive();
console.log("vehicle count is " + Car.vehicount);
</script>
</body>
</html>

Comments
Post a Comment