Avr*_*dis 7 javascript ecmascript-6
我正在使用Webstorm,我写了一个React组件,我的代码如下:
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;
}
Run Code Online (Sandbox Code Playgroud)
Webstorm的代码检查建议我Method can be static使用该onDragOver方法.我的问题是:
将该方法作为静态或这个建议在某种程度上无用是否有任何实际好处?
Mad*_*iha 11
是的,在调用静态函数时,您不需要该对象的实例.您只需要对构造函数的引用:
class Foo {
static bar() { console.log("foo"); }
}
Foo.bar(); // outputs "foo" to console
Run Code Online (Sandbox Code Playgroud)
无需new Foo()任何地方.
按照惯例,当您实际需要来自实例的状态(读取状态或写入状态)时,应该使用实例方法.
检查会告诉你,当你有没有原型/类方法this中它(并因此并不需要一个实例).
| 归档时间: |
|
| 查看次数: |
2893 次 |
| 最近记录: |