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 :: math power javascript 
Javascript :: Expected a JavaScript module script but the server responded with a MIME type of "text/html" 
Javascript :: mac os chrome opne debug new tab 
Javascript :: chrome storage local example 
Javascript :: javascript bind this to anonymous function 
Javascript :: Start Express Properly 
Javascript :: app bar in react native 
Javascript :: console vuex data 
Javascript :: javascriot function 
Javascript :: jquery class selector 
Javascript :: node js url download 
Javascript :: sublime javascript autocomplete 
Javascript :: base64 encode in javascript 
Javascript :: cancel or abort axios request 
Javascript :: js initialize array with values 
Javascript :: submit form without redirection 
Javascript :: console log 
Javascript :: is vowel javascript 
Javascript :: map function react 
Javascript :: how to use empty href link in reactjs 
Javascript :: get element by class name 
Javascript :: react native nav bars 
Javascript :: jquery datepicker 
Javascript :: javascript string proper case 
Javascript :: password regex 
Javascript :: react mid senior dev interview questuions 
Javascript :: ternary react 
Javascript :: axios in functional component 
Javascript :: put new attribute on html tag using javascript 
Javascript :: Prevent default event behavior 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =