Answers for "javascript class methods"

8

javascript create class

class Person {
 constructor(name, age) {
   this.name = name;
   this.age = age;
 }
  present = () => { //or present(){
  	console.log(`Hi! i'm ${this.name} and i'm ${this.age} years old`) 
  }
}
let me = new Person("tsuy", 15);
me.present();
// -> Hi! i'm tsuy and i'm 15 years old.
Posted by: Guest on May-10-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
10

javascript classes

//use classes by initiating one like so:
class MyClass {
	constructor(FirstProperty, SecondProperty, etcetera) {
    	//The constructor function is called with the new class 
      	//instance's parameters, so this will be called like so:
      	//var classExample = new MyClass("FirstProperty's Value", ...)
      this.firstProperty = FirstProperty;
      this.secondProperty = SecondProperty;
    }
  //creat methods just like functions:
  method(Parameters) {
  	//Code Here
  }
  //getters are properties that are calculated when called, versus fixed
  //variables, but still have no parenthesis when used
  get getBothValues() 
  {
  	return [firstProperty, secondProperty];
  }
}
//Note: this is all syntax sugar reducing the boilerplate versus a
// function-defined object.
Posted by: Guest on August-19-2020
0

get methods of class javascript

class Hi {
	constructor() {
      console.log("hi");
    }
   	my_method(){}
  	static my_static_method() {}
}

function getMethods(cl) {
  return Object.getOwnPropertyNames(cl.prototype)
}

console.log(getMethods(Hi)) // => [ 'constructor', 'my_method' ]
Posted by: Guest on October-11-2020
8

javascript class

class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
  // Getter
  get area() {
    return this.calcArea();
  }
  // Method
  calcArea() {
    return this.height * this.width;
  }
}

const square = new Rectangle(10, 10);

console.log(square.area); // 100
Posted by: Guest on May-31-2020
0

javascript class methods

class ClassName {

   constructor() { ... }

  method_1() { ... }

  method_2() { ... }

  method_3() { ... }

}
Posted by: Guest on May-24-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language