constructor in javascript
class Person {
constructor(name) {
this.name = name;
}
introduce() {
console.log(`Hello, my name is ${this.name}`);
}
}
constructor in javascript
class Person {
constructor(name) {
this.name = name;
}
introduce() {
console.log(`Hello, my name is ${this.name}`);
}
}
constructor in js
A constructor is a function that creates an instance of a class
which is typically called an “object”. In JavaScript, a constructor gets
called when you declare an object using the new keyword.
The purpose of a constructor is to create an object and set values if
there are any object properties present.
javascript construct
function Person(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
const member = new Person('Lydia', 'Hallie');
Person.getFullName = function() {
return `${this.firstName} ${this.lastName}`;
};
console.log(member.getFullName());
javascript constructor
// Let's make a function that will accept parameters for our vehicle
function Car(maker, model, color, tireSize) {
// Now we will set the paramaters equal to the different attributes of the car
this.maker = maker;
this.model = model;
this.color = color;
this.tireSize = tireSize;
// Next we'll make a method that will give a dynamic description of our car
this.description = function() {
return "My car is a " + color + " " + maker + " " + model + " with " + tireSize + " inch tires."
}
}
// Now let's make a new car. I want the car to have these attributes:
let car1 = new Car("Kia", "Soul", "white", 15);
// Now we can print the description of out new vehicle to the console
console.log(car1.description());
// OUTPUT: My car is a white Kia Soul with 15 inch tires.
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us