The basic use of the class 1 ES6

1. Review review Object-Oriented
        // //构造函数
        // function Point(){
        //     this.name = "张三";
        //     this.age = "18"
        // }

        // Point.prototype.eat = function(){
        //     console.log("大家都吃了吗?");
        // }
        
        // var p = new Point();     
        // console.log(p.name)
2. The concept of class

The concept of class as a template for creating objects by the class keyword to define a class
car category: Mercedes-Benz, BMW, Roewe
human: Europeans, Asians, Africans, minority ethnic
entire class inside the class constructor content equivalent to the above and the contents of the prototype aggregate
content constructor if not placed inside the constructor will automatically generate a write kongde
constructor which points to an instance of this object p

        class Point{
            constructor(){  //constructor 默认放在构造函数中
                this.name = "张三";
                this.age = "18";

            }
            eat(){    //原型(大多数方法都放原型里) 直接写 
                console.log('吃了么您')
            }
        }
        // 实例对象p    如果在实例化一个对象t那就指向t(this谁调用指向谁)
        let p = new Point(); // 类 结合new 构造器 实例化了一个对象p
                            // 对象里的内容包含了 构造函数 以及原型中的方法
        console.log(p.name);
        p.eat();
3. The following talk about mass participation issues
        class Point{
            constructor(name){  //形参 接收实参的数据 这里的实参只能在
                                // constructor里面用 ,如果原型里面想用
                                // 通过this.name = name;来保存(到this)
                this.name = name; //重要
                this.age = "18";

            }
            eat(){    //如果这里想 用
                console.log(`吃了么您${this.name}`)
            }

        let p = new Point("伤心太平洋"); //实参哪里调用那里传
                            
        console.log(p.name);
        p.eat();
4.set get keyword can set a custom name keywords used to keep the values ​​and value
        class Point{
            constructor(name,val){  //形参 
                this.name = name;
                this.age = "18";

                this.setData(val) //在函数内部也可以调用
            }
            eat(){    //
                console.log(`吃了么您${this.name}`)
            }
            // set prop(vale){
            //     console.log(`我的值是${vale}`)
            // }

            // get prop1(){ 
            //     return "非常时期"
            // }
            
            // 方法二推荐 自定义名称 向对应的更加灵活 
            setData(value){  //这种事在类的外部调用 ,在类的内部也可调用
                this.val = value;
                // console.log(this.val)
            }
            getData(){
                return this.val
            }
        }   
        let p = new Point("伤心太平洋","大西洋"); //实参哪里调用那里传                            
        console.log(p.name);
        p.eat();
        
        // p.prop = 1234;
        // console.log(p.prop1)

        // p.setData("我是第二种方法");
        console.log(p.getData());

Oh you need to watch the video message

Published 98 original articles · won praise 26 · views 7533

Guess you like

Origin blog.csdn.net/weixin_46146313/article/details/104234042