Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Javascript static

class Triple {
  static customName = 'Tripler';
  static description = 'I triple any number you provide';
  static calculate(n = 1) {
    return n * 3;
  }
}

class SquaredTriple extends Triple {
  static longDescription;
  static description = 'I square the triple of any number you provide';
  static calculate(n) {
    return super.calculate(n) * super.calculate(n);
  }
}

console.log(Triple.description);            // 'I triple any number you provide'
console.log(Triple.calculate());            // 3
console.log(Triple.calculate(6));           // 18

const tp = new Triple();

console.log(SquaredTriple.calculate(3));    // 81 (not affected by parent's instantiation)
console.log(SquaredTriple.description);     // 'I square the triple of any number you provide'
console.log(SquaredTriple.longDescription); // undefined
console.log(SquaredTriple.customName);      // 'Tripler'

// This throws because calculate() is a static member, not an instance member.
console.log(tp.calculate());                // 'tp.calculate is not a function'
Comment

static in javascript

/*
 * When using function constructors, you can create
 * a static property by simply using dot notation
 * on the constructor function.
 */
function Point(x, y) {
	this.x = x;
	this.y = y;
}
Point.dist = function(p1, p2) {
	return Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2));
};
Point.dist(new Point(0, 0), new Point(1, 0));
/*
 * When using classes, you can create a static
 * property using the static keyword.
 */
class Point {
	constructor(x, y) {
		this.x = x;
		this.y = y;
	}
	static dist(p1, p2) {
		return Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2));
	}
}
Point.dist(new Point(0, 0), new Point(1, 0));
Comment

JavaScript Static Methods

class Car {
  constructor(name) {
    this.name = name;
  }
  static hello() {
    return "Hello!!";
  }
}

let myCar = new Car("Ford");

// You can call 'hello()' on the Car Class:
document.getElementById("demo").innerHTML = Car.hello();

// But NOT on a Car Object:
// document.getElementById("demo").innerHTML = myCar.hello();
// this will raise an error.
Comment

PREVIOUS NEXT
Code Example
Javascript :: react native countdown 
Javascript :: js object filter by keys 
Javascript :: empty array javascript 
Javascript :: access the prototype of an object javascript 
Javascript :: how to call function from another component in vue js 
Javascript :: js brightness 
Javascript :: comentar en javascript 
Javascript :: how to assign an rest operator in javascript 
Javascript :: angular 9 features 
Javascript :: nextjs app 
Javascript :: printing in javascript 
Javascript :: js functional ajax requests 
Javascript :: first name last name concatenate javascript with ternary operator 
Javascript :: click function in js 
Javascript :: get array from string javascript 
Javascript :: react-native spinner 
Javascript :: function in javascript 
Javascript :: disable js in chrome dev tools 
Javascript :: how to store variable in local storage angualur 
Javascript :: set element at index javascript array and create new array 
Javascript :: material ui table row height 
Javascript :: javascript foreach call specific value in array 
Javascript :: Using the Set object 
Javascript :: break loop after time javascript 
Javascript :: javascript get last element in array 
Javascript :: TextInput cursor not shown react native 
Javascript :: check if string javascript 
Javascript :: never give up 
Javascript :: create three js webgl renderer 
Javascript :: ex: javascript loop array 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =