es5和es6实例化对象,静态成员,继承

	//ES5 实例化对象语法
	function Phone(brand, price){
        this.brand = brand;
        this.price = price;
    }

    //添加方法
    Phone.prototype.call = function(){
        console.log("我可以打电话!!");
    }

    //实例化对象
    let Huawei = new Phone('华为', 5999);
    Huawei.call();
    console.log(Huawei);
   // ES6 class类实例化对象语法
    class Shouji{
        //构造方法 名字不能修改
        constructor(brand, price){
            this.brand = brand;
            this.price = price;
        }

        //方法必须使用该语法, 不能使用 ES5 的对象完整形式
        call(){
            console.log("我可以打电话!!");
        }
    }

    let onePlus = new Shouji("1+", 1999);

    console.log(onePlus);
    console.log(onePlus.call());
	//es5静态成员
	function Phone(){
	
	}
	Phone.name = '手机';
	Phone.change = function(){
	    console.log("我可以改变世界");
	}
	Phone.prototype.size = '5.5inch';
	
	let nokia = new Phone();
	
	console.log(nokia.name);
	nokia.change();
	console.log(nokia.size);
	 //es6 静态成员
	class Phone{
        //静态属性
        static name = '手机';
        static change(){
            console.log("我可以改变世界");
        }
    }

    let nokia = new Phone();
    console.log(nokia.name);
    console.log(Phone.name);
	//es5继承
	//手机
	function Phone(brand, price){
	    this.brand = brand;
	    this.price = price;
	}
	
	Phone.prototype.call = function(){
	    console.log("我可以打电话");
	}
	
	//智能手机
	function SmartPhone(brand, price, color, size){
	    Phone.call(this, brand, price);
	    this.color = color;
	    this.size = size;
	}
	
	//设置子级构造函数的原型
	SmartPhone.prototype = new Phone;
	SmartPhone.prototype.constructor = SmartPhone;
	
	//声明子类的方法
	SmartPhone.prototype.photo = function(){
	    console.log("我可以拍照")
	}
	
	SmartPhone.prototype.playGame = function(){
	    console.log("我可以玩游戏");
	}
	
	const chuizi = new SmartPhone('锤子',2499,'黑色','5.5inch');
	
	console.log(chuizi);
	//es6继承
	class Phone{
        //构造方法
        constructor(brand, price){
            this.brand = brand;
            this.price = price;
        }
        //父类的成员属性
        call(){
            console.log("我可以打电话!!");
        }
    }

    class SmartPhone extends Phone {
        //构造方法
        constructor(brand, price, color, size){
            super(brand, price);// Phone.call(this, brand, price)
            this.color = color;
            this.size = size;
        }

        photo(){
            console.log("拍照");
        }

        playGame(){
            console.log("玩游戏");
        }

        call(){
            console.log('我可以进行视频通话');
        }
    }

    const xiaomi = new SmartPhone('小米',799,'黑色','4.7inch');
    console.log(xiaomi);
    xiaomi.call();
    xiaomi.photo();
    xiaomi.playGame();
	//子类对父类方法的重写
	class Phone{
        //构造方法
        constructor(brand, price){
            this.brand = brand;
            this.price = price;
        }
        //父类的成员属性
        call(){
            console.log("我可以打电话!!");
        }
    }

    class SmartPhone extends Phone {
        //构造方法
        constructor(brand, price, color, size){
            super(brand, price);// Phone.call(this, brand, price)
            this.color = color;
            this.size = size;
        }

        photo(){
            console.log("拍照");
        }

        playGame(){
            console.log("玩游戏");
        }

        call(){
            super.call();
            console.log('我可以进行视频通话');
        }
    }

    const xiaomi = new SmartPhone('小米',799,'黑色','4.7inch');
    console.log(xiaomi);
    xiaomi.call();
    xiaomi.photo();
    xiaomi.playGame();
  //class类的set和get
    class Phone{
        get price(){
            console.log("价格属性被读取了");
            return 'iloveyou';
        }

        set price(newVal){
            console.log('价格属性被修改了');
        }
    }

    //实例化对象
    let s = new Phone();

    // console.log(s.price);
    s.price = 'free';
上一篇:Java学习第七天


下一篇:工厂 模式