Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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"
Comment

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
Comment

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}
    })
Comment

PREVIOUS NEXT
Code Example
Javascript :: js read from json 
Javascript :: how to find whether empty or not using jQuery 
Javascript :: js write and get cookie 
Javascript :: how to make proptypes either or 
Javascript :: axios.post request with custom headers 
Javascript :: json with multiple objects 
Javascript :: multiple line string javascript 
Javascript :: js add to array if not exists 
Javascript :: jquery unfocus 
Javascript :: get first object key javascript 
Javascript :: discord.js verify 
Javascript :: input in javascript 
Javascript :: prettier/prettier in react 
Javascript :: async in useeffect 
Javascript :: React best way of forcing component to update 
Javascript :: js array fill map 
Javascript :: font ligature vs code 
Javascript :: js map add property 
Javascript :: first and last char vowel reg exp same char 
Javascript :: angular bind checkbox 
Javascript :: jquery find previous element with class 
Javascript :: history.push with params 
Javascript :: node js server get images from folder 
Javascript :: onloadscroll to top 
Javascript :: js remove item array 
Javascript :: create react portal 
Javascript :: how to put text in the center react native 
Javascript :: javascript extract number from string 
Javascript :: create a html table dynamically using javascript 
Javascript :: how to take input in javascript in coding 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =