Answers for "class constructor javascript"

5

javascript class constructor

class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
}

const person = new Person("John Doe", 23);

console.log(person.name); // expected output: "John Doe"
Posted by: Guest on May-20-2020
4

es6 class example

<script>
   class Student {
      constructor(rno,fname,lname){
         this.rno = rno
         this.fname = fname
         this.lname = lname
         console.log('inside constructor')
      }
      set rollno(newRollno){
         console.log("inside setter")
         this.rno = newRollno
      }
   }
   let s1 = new Student(101,'Sachin','Tendulkar')
   console.log(s1)
   //setter is called
   s1.rollno = 201
   console.log(s1)
</script>
Posted by: Guest on March-31-2020
18

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.
Posted by: Guest on May-22-2020
9

javascript class

// Improved formatting of Spotted Tailed Quoll's answer
class Person {
	constructor(name, age) {
		this.name = name;
		this.age = age;
	}
	introduction() {
		return `My name is ${name} and I am ${age} years old!`;
	}
}

let john = new Person("John Smith", 18);
console.log(john.introduction());
Posted by: Guest on May-25-2020
3

javascript class constructor

class Cat {
        constructor(name, age) {
            this.name = name;
            this.age = age;
        }
      
      	get fullname() {
          return this.getFullName()
        }
        getFullName() {
            return this.name + ' ' + this.age
        }
    }

    const run = document.getElementById("run");
    run.addEventListener("click", function () {
        let Skitty = new Cat('Skitty', 9);
        let Pixel = new Cat('Pixel', 6);
        console.log(Skitty.getFullName()); // Skitty 9
      	console.log(Skitty.fullname); // Skitty 9 => shorter syntax
        console.log(Skitty, Pixel); 
      // Object { name: "Skitty", age: 9} Object {name: "Pixel", age:6}
    })
Posted by: Guest on October-21-2020
0

class constructor javascript

class prettyMixedGirl {
    constructor(name, age, ethnicity, phoneNumber) {
        this.name = name;
        this.age = age;
        this.ethnicity = ethnicity;
        this.phoneNumber = phoneNumber;
    }
    // Method
    hi() {
        console.log(`Hi! My name is ${this.name}. I am ${this.age} years old. My ethnicity is ${this.ethnicity}. My phone number is ${this.phoneNumber}`);
    }
}
// Create new object out of Constructor (Instantiate)
const ashley = new prettyMixedGirl('Ashley', 28, 'Dominican Republican', '313-218-1345');
const luna = new prettyMixedGirl('Luna', 26, 'Chilean', '718-231-1343');

// Initiate a method
ashley.hi(); // Hi! My name is Ashley. I am 28 years old. My ethnicity is Dominican Republican. My phone number is 313-218-1345
luna.hi(); // Hi! My name is Luna. I am 26 years old. My ethnicity is Chilean. My phone number is 718-231-1343
Posted by: Guest on July-28-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language