javascript - Purpose of Static Methods in ECMAScript 6 Classes -


what sort of issues es5 static class methods in es6 supposed deal with?

the babel documentation has following example in its section regarding es6 classes, though not state pattern accomplishes.

classes support prototype-based inheritance, super calls, instance , static methods , constructors

class skinnedmesh extends three.mesh {   constructor(geometry, materials) {     super(geometry, materials);      this.idmatrix = skinnedmesh.defaultmatrix();     this.bones = [];     this.bonematrices = [];     //...   }   update(camera) {     //...     super.update();   }   static defaultmatrix() {     return new three.matrix4();   } } 

if compile es6 code babel, , class contains static method, es5-generated code adding static function constructor function.

so, es6 code:

class {    static dostuff() {} } 

...equals (in es5):

function a() { } a.dostuff = function() { }; 

why need static functions? well, transpiled code won't support statics @ all, since functions objects , static functions converted constructor function object functions.

static functions or properties can used implement factory pattern:

class {    static create() {       // specific factory method code    }  }  var instance = a.create(); 

anyway, static member usage diffuse topic , goes out of scope of objective answer. has lot of use cases , these universal programming language.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -