久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長(zhǎng)資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      es6中的class有沒有靜態(tài)屬性

      es6中的class沒有靜態(tài)屬性。靜態(tài)屬性是class本身的屬性,即直接定義在類內(nèi)部的屬性( Class.propname ),不需要實(shí)例化;但ES6中規(guī)定,Class內(nèi)部只有靜態(tài)方法,沒有靜態(tài)屬性。

      es6中的class有沒有靜態(tài)屬性

      本教程操作環(huán)境:windows7系統(tǒng)、ECMAScript 6版、Dell G3電腦。

      在ES6中,class (類)作為對(duì)象的模板被引入,可以通過 class 關(guān)鍵字定義類。

      class 的本質(zhì)是 function。

      它可以看作一個(gè)語法糖,讓對(duì)象原型的寫法更加清晰、更像面向?qū)ο缶幊痰恼Z法。

      ES6 Class 靜態(tài)方法、屬性和實(shí)例屬性

      類相當(dāng)于實(shí)例的原型, 所有在類中定義的方法, 都會(huì)被實(shí)例繼承。 如果在一個(gè)方法前, 加上static關(guān)鍵字, 就表示該方法不會(huì)被實(shí)例繼承, 而是直接通過類來調(diào)用, 這就稱為“ 靜態(tài)方法”。

      class Foo { 	static classMethod() { 		return 'hello'; 	} } Foo.classMethod() // 'hello' var foo = new Foo(); foo.classMethod() 	// TypeError: foo.classMethod is not a function
      登錄后復(fù)制

      上面代碼中, Foo類的classMethod方法前有static關(guān)鍵字, 表明該方法是一個(gè)靜態(tài)方法, 可以直接在Foo類上調(diào)用( Foo.classMethod()), 而不是在Foo類的實(shí)例上調(diào)用。 如果在實(shí)例上調(diào)用靜態(tài)方法, 會(huì)拋出一個(gè)錯(cuò)誤, 表示不存在該方法。
      父類的靜態(tài)方法, 可以被子類繼承。

      class Foo { 	static classMethod() { 		return 'hello'; 	} } class Bar extends Foo {} Bar.classMethod(); // 'hello'
      登錄后復(fù)制

      上面代碼中, 父類Foo有一個(gè)靜態(tài)方法, 子類Bar可以調(diào)用這個(gè)方法。

      靜態(tài)方法也是可以從super對(duì)象上調(diào)用的。

      class Foo { 	static classMethod() { 		return 'hello'; 	} } class Bar extends Foo { 	static classMethod() { 		return super.classMethod() + ', too'; 	} } Bar.classMethod();
      登錄后復(fù)制

      靜態(tài)屬性

      靜態(tài)屬性指的是 Class 本身的屬性, 即Class.propname, 而不是定義在實(shí)例對(duì)象( this) 上的屬性。

      class Foo {} Foo.prop = 1; Foo.prop // 1
      登錄后復(fù)制

      上面的寫法為Foo類定義了一個(gè)靜態(tài)屬性prop。

      目前, 只有這種寫法可行, 因?yàn)?ES6 明確規(guī)定, Class 內(nèi)部只有靜態(tài)方法, 沒有靜態(tài)屬性。

      //  以下兩種寫法都無效 class Foo { 	//  寫法一 	prop: 2 		//  寫法二 	static prop: 2 } Foo.prop // undefined
      登錄后復(fù)制

      ES7 有一個(gè)靜態(tài)屬性的提案, 目前 Babel 轉(zhuǎn)碼器支持。

      這個(gè)提案對(duì)實(shí)例屬性和靜態(tài)屬性, 都規(guī)定了新的寫法。

      ( 1) 類的實(shí)例屬性

      類的實(shí)例屬性可以用等式, 寫入類的定義之中。

      class MyClass { 	myProp = 42; 	constructor() { 		console.log(this.myProp); // 42 	} }
      登錄后復(fù)制

      上面代碼中, myProp就是MyClass的實(shí)例屬性。 在MyClass的實(shí)例上, 可以讀取這個(gè)屬性。
      以前, 我們定義實(shí)例屬性, 只能寫在類的constructor方法里面。

      class ReactCounter extends React.Component { 	constructor(props) { 		super(props); 		this.state = { 			count: 0 		}; 	} }
      登錄后復(fù)制

      上面代碼中, 構(gòu)造方法constructor里面, 定義了this.state屬性。
      有了新的寫法以后, 可以不在constructor方法里面定義。

      class ReactCounter extends React.Component { 	state = { 		count: 0 	}; }
      登錄后復(fù)制

      這種寫法比以前更清晰。

      為了可讀性的目的, 對(duì)于那些在constructor里面已經(jīng)定義的實(shí)例屬性, 新寫法允許直接列出。

      class ReactCounter extends React.Component { 	constructor(props) { 		super(props); 		this.state = { 			count: 0 		}; 	} 	state; }
      登錄后復(fù)制

      (2) 類的靜態(tài)屬性

      類的靜態(tài)屬性只要在上面的實(shí)例屬性寫法前面, 加上static關(guān)鍵字就可以了。

      class MyClass { static myStaticProp = 42; constructor() { console.log(MyClass.myProp); // 42 } }
      登錄后復(fù)制

      同樣的, 這個(gè)新寫法大大方便了靜態(tài)屬性的表達(dá)。

      //  老寫法 class Foo {} Foo.prop = 1; //  新寫法 class Foo { 	static prop = 1; }
      登錄后復(fù)制

      上面代碼中, 老寫法的靜態(tài)屬性定義在類的外部。 整個(gè)類生成以后, 再生成靜態(tài)屬性。 這樣讓人很容易忽略這個(gè)靜態(tài)屬性, 也不符合相關(guān)代碼應(yīng)該放在一起的代碼組織原則。 另外, 新寫法是顯式聲明( declarative), 而不是賦值處理, 語義更好。

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)