javascript - Is there any advantage of having a method as static? -


i using webstorm , wrote react component, code looks like:

async ondrop( banner, e ) {     banner.classlist.remove( 'dragover' );     e.preventdefault();      const file = e.datatransfer.files[ 0 ], reader = new filereader();     const { dispatch } = this.props;      const result = await this.readfile( file, reader );      banner.style.background = `url( ${ result } ) no-repeat center`;     dispatch( addlayer( file ) );      return false; }   @isimage( 0 ) readfile( file, reader ) {     reader.readasdataurl( file );      return new promise( function ( resolve, reject ) {         reader.onload = ( event ) => resolve( event.target.result );         reader.onerror = reject;     } ); }   ondragover( banner ) {     banner.classlist.add( 'dragover' );     return false; } 

webstorm's code inspection suggest me method can static ondragover method. question is:

are there real benefit having method static or suggestion somehow useless?

yes, don't need instance of object when invoking static function. need reference constructor:

class foo {   static bar() { console.log("foo"); } }  foo.bar(); // outputs "foo" console 

no need new foo() anywhere.

by convention, instance methods should used when need state (either read state, or write state) instance.

the inspection tell when have prototype/class method not have this in (and doesn't need instance).


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -