Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

static js

// the static method is a method which cannot be called through a class instance.
class Point {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }

  static distance(a, b) {
    const dx = a.x - b.x;
    const dy = a.y - b.y;

    return Math.hypot(dx, dy);
  }
}

const p1 = new Point(7, 2);
const p2 = new Point(3, 8);

console.log(Point.distance(p1, p2));
Comment

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

PREVIOUS NEXT
Code Example
Javascript :: jquery select input 
Javascript :: add next to react 
Javascript :: how to add lang attribute in next js 
Javascript :: cordova delete cache 
Javascript :: how to install nide js in ubuntu 
Javascript :: form submit not reaload 
Javascript :: react native showing double header stack and drawer menu 
Javascript :: javascript es6 class 
Javascript :: geojson 
Javascript :: javascript onclick event 
Javascript :: react native clear route params 
Javascript :: javascript module pattern 
Javascript :: useScroll 
Javascript :: vscode js intellisence not working 
Javascript :: vuejs transform observer to object 
Javascript :: innerhtml 
Javascript :: validate email or phone js 
Javascript :: how to pass a prop in route 
Javascript :: nodejs create tree from directories 
Javascript :: set body id js 
Javascript :: js fetch json 
Javascript :: js fetch 
Javascript :: node.js error handling process 
Javascript :: fibonacci recursion 
Javascript :: on scroll call function jquery 
Javascript :: array index javascript show only first 2 elements 
Javascript :: javascript check if element has specific child 
Javascript :: react arrow function component 
Javascript :: fetch data with axios in reactjs 
Javascript :: how to use if else inside jsx in react 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =