ES6 - 初步了解类

类的基本语法

什么是类

类,作为创建对象的模板,通过 class 关键字,来定义
其实更像是将 构造函数 / 原型 集中在一起 全部表达出来
constructor 就好比构造函数
在这里插入图片描述

类写法

注意:

  • class关键字来声明
  • constructor必须写,不写会自动生成空白的 constructor
  • constructor 中 this 指向调用对象,实例化的对象
    指代的可以是整个class类
  • constructor 外部的函数内想要调用 constructor内部的参数时
    必须在 constructor 中保存(通过 this 保存)
    class Animal {
		// 必须写,不写会自动生成空白的 constructor 
		constructor(name,age){ //类似构造函数
		
                // this 指向调用对象,实例化的对象
                // 指代的可以是整个 Animal 的 this
                this.name = name;
                this.age = age;
            }

            // 类似原型中的方法
            eat (){
                // 想要调用name,必须在constructor中保存(this保存)
                console.log(`我叫${this.name},我还不饿`)
            }
            
	}
        
	var dog = new Animal("dog",11);

	console.log(dog.name)
	dog.eat()
	
设置 / 获取 数据
  • 方法1:
    set / get 关键字声明函数
    class Animal {
		constructor(name,age){ 
                this.name = name;
                this.age = age;
            }
        eat (){
			console.log(`我叫${this.name},我还不饿`)
        }
        
        set talk(num){
            console.log(num)
        }

        get say(){
            return "hello"
        }
	}
        
	var dog = new Animal("dog",11);

    dog.talk = 24;
    console.log(dog.say);
  • 方法2:
    setData / getData 声明函数
    class Animal {
    	// 注意保存 boom
		constructor(name,age,boom){ 
                this.name = name;
                this.age = age;
                //设置data 可以再这里调用 
                this.setData(boom);
            }
        eat (){
			console.log(`我叫${this.name},我还不饿`)
        }
        
        // 设置数据
        setData(pop){ // pop 只是作为形参
			this.pop = `新来的` + pop;
            console.log(this.pop)
		}
		// 获取数据
        getData(){
            return this.pop
        }

	}
        
	var dog = new Animal("dog",11,"boom");

    dog.setData("生命");
    // 放在constructor 中的setData boom
    console.log(dog.getData());
    
属性表达式

将方法名也设置成一个灵活的值 在类的外部写
使属性名变得灵活

    { 
        // 实际意义上的函数名是shop
        let shopping = "shop";
        
        class Person{

            constructor(sex,hobby){
                this.sex = sex;
                this.hobby ="money";
            }

            [shopping](){
                console.log('买买买')
            }

        }

        let person = new Person();
		// shop 作为函数名去调用
        person.shop();

    }
class表达式

使用较少

    {
        // class 表达式 使用较少
        const myClass = class Me{
            // 在内部时,可以使用 me 去调用

        }
        
        // 在外部时使用myClass调用
    } 
    
严格模式
  • 注意点:

    • 在ES6中,默认的就是严格模式,所以不需要使用 “use strict” 来指定运行模式
    • 不存在变量提升
    • 类和模块 { } 的内部,默认就是严格模式,其实未来所有的代码都是运行在模块中
      所以说ES6实际上把整个语言升级到了严格模式
  • 对变量的影响

    • 变量必须要先声明再使用
    • 不可以使用任意保留字/关键字作为变量名
    • 严格模式不再允许用户删除变量 ( delete )
  • 对 对象的影响

    • 对只读属性 赋值 会抛出异常
    • 对不可配置的属性使用 delete操作符会抛出异常
    • 对不可扩展的对象添加属性,会抛出异常
    • 不可以使用with简化对象访问
  • 对函数的影响

    • 在普通函数当中,不允许使用this引用当前的 window 对象
    • 在严格模式中,函数的参数必须是唯一的 ( 也就说不允许有重复的 )
    • 在严格模式中,arguments 对象的值,和形参的值是完全独立的,虽然修改形参的值也会反映到 arguments 对象中
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值