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

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 :: how to pass functions as a props in react js 
Javascript :: Search by text score in mongodb 
Javascript :: js react 
Javascript :: mongoose query object 
Javascript :: clone an object javascript 
Javascript :: nodejs controller 
Javascript :: javascript code for find the last element in array 
Javascript :: array of objects in js 
Javascript :: how to console log in react native 
Javascript :: how reducer works in redux 
Javascript :: last value of array 
Javascript :: code splitting react 
Javascript :: setimmediate javascript 
Javascript :: npm windows registry 
Javascript :: null check in javascript 
Javascript :: vuetify select array 
Javascript :: array of objects in javascript 
Javascript :: javascript sandbox 
Javascript :: setjavascriptenabled why it is used 
Javascript :: google chart ajax json 
Javascript :: move li to bottom of list jquery selected value 
Javascript :: cuantos docentes hay en mexico 
Javascript :: js array to scsv 
Javascript :: how to filter on a hidden column datatables 
Javascript :: NativeKeyboard - NativeKeyboard - NativeKeyboard - NativeKeyboard]: NullInjectorError: No provider for NativeKeyboard! 
Javascript :: how to clear screen in vis code 
Javascript :: eeeeee 
Javascript :: js regex to find string but not another 
Javascript :: renderer.setElementStyle 
Javascript :: Duplicate module name: React Native hasteimpl react native android 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =