31
loading...
This website collects cookies to deliver better user experience
function Vehicle(vinNumber, manufacturer, productionDate, fuelType) {
this.manufacturer = manufacturer;
this.vinNumber = vinNumber;
this.productionDate = productionDate;
this.fuelType = fuelType;
}
Vehicle.prototype.vehicleInformation = function() {
var productionDate = new Date(this.productionDate * 1000);
var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var year = productionDate.getFullYear();
var month = months[productionDate.getMonth()];
var day = productionDate.getDate();
var friendlyDate = month + ' ' + day + ', ' + year;
return this.manufacturer + ' vehicle with VIN Number = ' + this.vinNumber + ' was produced on ' + friendlyDate + ' using a fuel type of ' + this.fuelType;
}
Vehicle
object available and a new instance can be created using the following code:let rogue = new Vehicle('5N1FD4YXN11111111', 'Nissan', 1389675600, 'gasoline');
vehicleInformation()
function can be called using the following approach:alert(rogue.vehicleInformation());
"Nissan vehicle with VIN Number = 5N1FD4YXN11111111 was produced on Jan 14, 2014 using a fuel type of gasoline"
SportUtilityVehicle
can be introduced to further define a given type of vehicle:function SportUtilityVehicle(vinNumber, manufacturer, productionDate, fuelType, drivetrain) {
Vehicle.call(this, vinNumber, manufacturer, productionDate, fuelType);
this.drivetrain = drivetrain;
}
SportUtilityVehicle
instead of a simple Vehicle
.let rogue = new SportUtilityVehicle('5N1FD4YXN11111111', 'Nissan', 1389675600, 'gasoline', 'AWD');
SportUtilityVehicle
prototype:SportUtilityVehicle.prototype.vehicleInformation = function() {
return this.manufacturer + ' vehicle with VIN Number = ' + this.vinNumber + ' utilizes drivetrain = ' + this.drivetrain + ' and runs on ' + this.fuelType;
}
vehicleInformation()
function is called using the following approach:alert(rogue.vehicleInformation());
"Nissan vehicle with VIN Number = 5N1FD4YXN11111111 utilizes drivetrain = AWS and runs on gasoline"
class Vehicle {
constructor(vinNumber, manufacturer, productionDate, fuelType) {
this.manufacturer = manufacturer;
this.vinNumber = vinNumber;
this.productionDate = productionDate;
this.fuelType = fuelType;
}
vehicleInformation() {
var productionDate = new Date(this.productionDate * 1000);
var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var year = productionDate.getFullYear();
var month = months[productionDate.getMonth()];
var day = productionDate.getDate();
var friendlyDate = month + ' ' + day + ', ' + year;
return this.manufacturer + ' vehicle with VIN Number = ' + this.vinNumber + ' was produced on ' + friendlyDate + ' using a fuel type of ' + this.fuelType;
}
}
class SportUtilityVehicle extends Vehicle {
constructor(vinNumber, manufacturer, productionDate, fuelType, drivetrain) {
super(vinNumber, manufacturer, productionDate, fuelType);
this.drivetrain = drivetrain;
}
vehicleInformation() {
return this.manufacturer + ' vehicle with VIN Number = ' + this.vinNumber + ' utilizes drivetrain = ' + this.drivetrain + ' and runs on ' + this.fuelType;
}
}
SportUtilityVehicle
class, the class can be updated as shown below:class SportUtilityVehicle extends Vehicle {
constructor(vinNumber, manufacturer, productionDate, fuelType, drivetrain) {
super(vinNumber, manufacturer, productionDate, fuelType);
this.drivetrain = drivetrain;
}
vehicleInformation() {
return this.manufacturer + ' vehicle with VIN Number = ' + this.vinNumber + ' utilizes drivetrain = ' + this.drivetrain + ' and runs on ' + this.fuelType;
}
get drivetrain() {
return this._drivetrain;
}
set drivetrain(newDrivetrain) {
this._drivetrain = newDrivetrain;
}
}
Binding issues. As class constructor functions deal closely with this keyword, it can introduce potential binding issues, especially if you try to pass your class method as a callback to an external routine.
"Focus your time on delivering features/functionality which extends the value of your intellectual property. Leverage frameworks, products, and services for everything else."
import { LightningElement } from 'lwc';
export default class VehicleComponent extends LightningElement {
// properties go here
vehicleInformation() {
return this.manufacturer + ' vehicle with VIN Number = ' + this.vinNumber + ' utilizes drivetrain = ' + this.drivetrain + ' and runs on ' + this.fuelType;
}
}