1.static —>静态变量或静态方法
- 静态变量是不能被改变的
- 静态方法是不能被重写的
- 静态方法直接通过类名调用—>(方便调用)
- 静态方法中不能调用非静态的方法
<script>
class Person{
sayAge(){
console.log(18)
}
static sayName(){
this.sayAge()
console.log("hello")
}
}
var p = new Person();
Person.sayName()
</script>
2.static-extends
<srcipt>
class person(){
static request(){
console.log("hello world")
}
}
class student extends person{
static getTop250(){
this.request()
}
}
</script>
2.1例子—请求网易云—华语
HTTP.js
var baseUrl="https://music.aityp.com/";
class HTTP{
static request({url,data}){
return new Promise((resolve,reject)=>{
wx.request({
url:baseUrl+url,
data,
header:{'content-type':'aplication/json'},
method:'GET',
dataType:'json',
responseType:'text',
success:(res)=>{
resolve(res)
},
fail:(err)=>{
reject(err)
}
});
}
module.exports=HTTP;
IndexModel.js
const HTTP=require('./HTTP')
class IndexModel extends HTTP{
static getMusic(){
return this.request({
url:'top/playlist',
data:{
cat:"华语"
}
})
}
}
module.expots=IndexModel;