how to create a constructor function in javascript code example
Example 1: metodo constructor javascript
class miClase {
constructor(primero, segundo) {
this.uno = primero;
this.dos = segundo;
this.tres = "Hola a todos";
};
miMetodoUno() {
return this.dos;
}
miMetodoDos(parametroUno, parametroDos) {
console.log(
"El parametro UNO es: " + parametroUno +
"<br>El parametro DOS es: " + parametroDos
)
}
}
var objetoUno = new miClase("Antonio", 56);
objetoUno.miMetodoDos("Manuel", 44);
Example 2: javascript constructor function
function ClassMates(name,age){
this.name=name;
this.age=age;
this.displayInfo=function(){
return this.name + "is " + this.age + "year's old!";
}
}
let classmate1 = new ClassMates("Mike Will", 15);
classmate.displayInfo();
Example 3: js constructor object
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
var car1 = new Car('Eagle', 'Talon TSi', 1993);
console.log(car1.make);
Example 4: javascript constructor object
function Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}
Example 5: JavaScript constructor function
function Person () {
this.name = 'John',
this.age = 23
}
const person = new Person();