javascript class
JavaScript
class ClassMates{
constructor(name,age){
this.name=name;
this.age=age;
}
displayInfo(){
return this.name + "is " + this.age + " years old!";
}
}
let classmate = new ClassMates("Mike Will",15);
classmate.displayInfo(); // result: Mike Will is 15 years old!//JavaScript class: Here a quick code example
// Basic class
class Rectangle {
// Constructor
constructor(height, width) {
// Member variables
this.height = height;
this.width = width;
// Access static member variable
Rectangle.count++;
}
// Getter
get area() {
return this.calcArea();
}
// Method
calcArea() {
return this.height * this.width;
}
// Static method
static calcArea(width, height) {
return width * height;
}
}
// Static member variable
Rectangle.count = 0;
// Class instantiation
const square = new Rectangle(10, 10);
// Access member variable
console.log(square.height, square.width); // 10 10
// Call getter
console.log(square.area); // 100
// Call method
console.log(square.calcArea()); // 100
// Access static member variable
console.log(Rectangle.count); // 1
// Call static method
console.log(Rectangle.calcArea(15, 15)); // 225// 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());let Person = class {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
}class Car {
constructor(brand) {
this.carname = brand;
}
present() {
return "I have a " + this.carname;
}
}
/*
classes are just fancy constructors
they have some amazing feutures
in a class the contructor function get's called
when creating an new instance of that class*/
var mycar = new Car("dikke bmw");
mycar.present();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
Also in JavaScript:
- Title
- drupal 8 check if current page is node
- Category
- JavaScript
- Title
- functions in map javascript
- Category
- JavaScript
- Title
- get start of day javascript
- Category
- JavaScript
- Title
- initialize state react
- Category
- JavaScript
- Title
- get month in two digit in javascript date
- Category
- JavaScript
- Title
- excel javascript api protect worksheet
- Category
- JavaScript
- Title
- how to find the index of a value in an array in javascript
- Category
- JavaScript
- Title
- delete previous line node
- Category
- JavaScript
- Title
- extend react.component
- Category
- JavaScript
- Title
- .push js
- Category
- JavaScript
- Title
- change js to json
- Category
- JavaScript
- Title
- how to add row in angular dynamically
- Category
- JavaScript
- Title
- check unique object in array javascript site:stackoverflow.com
- Category
- JavaScript
- Title
- Enclose in Brackets
- Category
- JavaScript
- Title
- how to import jquery in js file
- Category
- JavaScript
- Title
- add elements to an array with splice
- Category
- JavaScript
- Title
- javascript ascii to hex
- Category
- JavaScript
- Title
- compile angularjs template
- Category
- JavaScript
- Title
- initialize a map js
- Category
- JavaScript
- Title
- javascript access pushed element
- Category
- JavaScript
- Title
- clearinterval javascript
- Category
- JavaScript
- Title
- html decode javascript
- Category
- JavaScript
- Title
- iframe reload parent page
- Category
- JavaScript
- Title
- delete js
- Category
- JavaScript
- Title
- how to convert time to am pm in javascript
- Category
- JavaScript
- Title
- date picker javascript not working
- Category
- JavaScript
- Title
- empty div jquery
- Category
- JavaScript
- Title
- get selected option value
- Category
- JavaScript
- Title
- get the value of a checkbox jquery
- Category
- JavaScript
- Title
- how to add validator to formgroup
- Category
- JavaScript
- Title
- how to get data from localstorage in javascript
- Category
- JavaScript
- Title
- implementing node js app on server and runing npm
- Category
- JavaScript
- Title
- html to jsx
- Category
- JavaScript
- Title
- faunadb ref
- Category
- JavaScript
- Title
- javascript advanced concepts
- Category
- JavaScript
- Title
- get current directory vbscript
- Category
- JavaScript
- Title
- javascript array flat
- Category
- JavaScript
- Title
- document delete element
- Category
- JavaScript
- Title
- explode in jquery
- Category
- JavaScript
- Title
- get utc time javascript
- Category
- JavaScript
- Title
- get selected text js
- Category
- JavaScript
- Title
- enable disable click on div jquery
- Category
- JavaScript
- Title
- autocomplete list angular 8 material
- Category
- JavaScript
- Title
- install node modules
- Category
- JavaScript
- Title
- comparing arrays javascript
- Category
- JavaScript
- Title
- injected stylesheet remove
- Category
- JavaScript
- Title
- compare two arrays javascript
- Category
- JavaScript
- Title
- boucle for in js
- Category
- JavaScript
- Title
- how to initialize an array in javascript
- Category
- JavaScript
- Title
- addEventListener
- Category
- JavaScript
- Title
- how to import npm packages in node.js?
- Category
- JavaScript
- Title
- copy to clipboard javascript
- Category
- JavaScript
- Title
- how to remove a property from an object in javascript
- Category
- JavaScript
- Title
- conditional rendering in react
- Category
- JavaScript
- Title
- how to make a property important in javascript
- Category
- JavaScript
- Title
- combine p5 with react
- Category
- JavaScript
- Title
- group all items with same name js
- Category
- JavaScript
- Title
- express server replit
- Category
- JavaScript
- Title
- chart js rotating the x axis labels
- Category
- JavaScript
- Title
- for loop javascript
- Category
- JavaScript
- Title
- An unhandled exception occurred: Script file ../node_modules/jquery/dist/jquery.min.js does not exist.
- Category
- JavaScript
- Title
- adding cors parameters to extjs ajax
- Category
- JavaScript
- Title
- access to static file nodejs
- Category
- JavaScript
- Title
- how to create a 30 seconds timer using js
- Category
- JavaScript
- Title
- javascript ajax post send an object
- Category
- JavaScript
- Title
- check if string is datestring javascript
- Category
- JavaScript
- Title
- how to parse header in node.js lambda
- Category
- JavaScript
- Title
- javascript arrow function
- Category
- JavaScript
- Title
- how to loop through array of numbers in javascript
- Category
- JavaScript
- Title
- how to get session javascript ws3schools
- Category
- JavaScript
- Title
- conditional rendering with radio buttons
- Category
- JavaScript
- Title
- icon shwoing a box react native vector icons
- Category
- JavaScript
- Title
- get input value in angular 7
- Category
- JavaScript
- Title
- get unique id angular
- Category
- JavaScript
- Title
- await async sleep
- Category
- JavaScript
- Title
- javascript alert
- Category
- JavaScript
- Title
- https package node post request
- Category
- JavaScript
- Title
- first element of array js
- Category
- JavaScript
- Title
- how to make a tile generation system in javascript
- Category
- JavaScript
- Title
- find intersection between two object arrays javascript
- Category
- JavaScript
- Title
- convert json to object jackson
- Category
- JavaScript
- Title
- javascript 2 decimal float array elements
- Category
- JavaScript
- Title
- align left text in reactstrap
- Category
- JavaScript
- Title
- empty array javascript
- Category
- JavaScript
- Title
- insert json into sql
- Category
- JavaScript
- Title
- angular calendar updating until hovering
- Category
- JavaScript
- Title
- how to return a string x amount in javascript without using . repeat
- Category
- JavaScript
- Title
- for loop js example
- Category
- JavaScript
- Title
- fetch composition API in Vue3
- Category
- JavaScript
- Title
- ckeditor change value
- Category
- JavaScript
- Title
- get page resolution jquery
- Category
- JavaScript
- Title
- get all the child of the same class javascript
- Category
- JavaScript
- Title
- google auth.onstatechange
- Category
- JavaScript
- Title
- convert milliseconds to time javascript
- Category
- JavaScript
- Title
- Heroku H10-App Crashed Error
- Category
- JavaScript
- Title
- how to open bash_profile
- Category
- JavaScript
- Title
- how to substract date object javascript
- Category
- JavaScript
- Title
- how do I use const in javascript
- Category
- JavaScript
- Title
- express() vs express.router()
- Category
- JavaScript
- Title
- javascript anagram check
- Category
- JavaScript