factory function javascript
function createPerson(firstName, lastName) {
return {
firstName: this.firstName,
lastName: this.lastName,
getFullName: function () {
return `${this.firstName} ${this.lastName}`
}
}
}
let user = createPerson('John', 'Doe');
console.log(user.getFullName()); // John Doe// constructor
function ConstructorCar () {}
ConstructorCar.prototype.drive = function () {
console.log('Vroom!');
};
const car2 = new ConstructorCar();
console.log(car2.drive());
// factory
const proto = {
drive () {
console.log('Vroom!');
}
};
const factoryCar = () => Object.create(proto);
const car3 = factoryCar();
console.log(car3.drive());
// class
class ClassCar {
drive () {
console.log('Vroom!');
}
}
const car1 = new ClassCar();
console.log(car1.drive());
Also in JavaScript:
- fizzbuzz js
- javascript get second last element in array
- refresh data after some time angular
- filter an array of objects and match its key with values inside another array
- Write a javascript program to find roots of quadratic equation.
- how to know how many pixels of page be scrolled javascript
- math js
- adding int and string in react props
- class extends
- Texto unitário no node js
- find even numbers in an array javascript
- Aspect Ratio height react
- check if enter key is pressed jquery
- javascript localstorage
- how could you implement javascript into java
- js data types
- jquery validation plugin
- how to get code suggestions for react components
- jquery datepicker
- geolocation speed
- jquery hover and hover out
- discord.js v12 how to set owner commands
- javascript download current html page
- how to get csrf token in javascript