es6 Class基本语法一

class 使用与构造函数基本相同

1.基本概念

class point {
	toString(){}
	constructor(){
    	this.b = 5
    	this.a = function(){}
	}
}
let p = new point()

1.class里的方法都写在原型上 用Object.keys§是检测不到的
2.typeof point ‘function’
3.类内部的方法都是不可枚举
4.类内部不允许使用严格模式
5.如果一个属性方法没有this,那就是原型上的方法或属性 如上的例子原型上只有一个toString,属性b和方法a可以在实例对象上直接继承,但是要写this,就要在constructor上写

2.类的this指向问题

class point{
	constructor(){
     	this.b = 5
     	this.a = function(){}
     	this.printName = this.print.bind(this)
     }
    print(){
       console.log('print')
       return 1
    }
}
let p = new point()
let result = p.printName
result() // 输出print 返回 1
//这个时候虽然是window在调用该方法,但this被修改指向了point实例

或者使用箭头函数

class point{
	constructor(){
     	this.b = 5
     	this.a = function(){}
     	this.printName = () => {this.print()}
     }
    print(){
       console.log('print')
       return 1
    }
}
let p = new point()
let result = p.printName
result() // 输出print 返回 1

3.类的取值和存值

        class point {
            toString(){}
           
           get prop(){
               return true
           }
           set prop(value){
               console.log(123)
               
           }
           
        }
        let p = new point()
        
        console.log(p.prop) // 返回结果为true
        
        p.prop = 5  //输出123 证明函数已经执行了

4.class的静态方法和属性

        class point {
            toString(){}
           constructor(){
               this.a = 10
               this.b = 20
           }
            static classMethod(){
                return true
            }
            static h = 50
        }
        let p = new point()
       
         console.log(p.h)   //undefined
          p.classMethod()   //报错

写了一个static声明后的,就只是class的静态属性不能被实例对象捕捉到

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值