Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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));
Source by developer.mozilla.org #
 
PREVIOUS NEXT
Tagged: #static #javascript
ADD COMMENT
Topic
Name
3+6 =