es6第二天作业

1、

假设:

class Person{

                            constructor(name,sex,age) {

                                this.name = name;

                                   this.sex = sex;

                                   this.age = age;

                            }

                            login(){

                                   console.log("您登陆了。。。");

                            }

                     }

                    

                     class Student extends Person{

                            constructor(name,sex,age,id) {

                                super(name,sex,age);

                                   this.id = id;

                            }

                     }

画出 object person student原型链关系图

答:

2、编写程序使用ES6定义 Person类,包括类实例属性(name,age),实例方法say()该方法

返回name和age字符串

class Person{

                            constructor(name,age) {

                                this.name = name;

                                   this.age = age;

                            }

                            say(){

                                   console.log("My name is:"+this.name+",age is :"+this.age);

                            }

                     }

3、下面程序执行结果为:

 var p=new Person();

  console.log(p.__proto__===Person.prototype)

答:返回true

 

4、下面程序正确吗?错在哪里?如何改正?

class Point {

  constructor(x, y) {

    this.x = x;

    this.y = y;

  }

}

class ColorPoint extends Point {

          constructor(x, y, color) {

       this.color = color; // ReferenceError

       super(x, y);

           }

}

var cp=new ColorPoint(10,20,'red');

答:不正确。错在:

constructor(x, y, color) {

       this.color = color; // ReferenceError

       super(x, y);

           }

改正:

constructor(x, y, color) {

       super(x, y);

       this.color = color; // ReferenceError

           }

5、下面程序执行结果为?

class Parent {

  static myMethod(msg) {

    console.log('static', msg);

  }

  myMethod(msg) {

    console.log('instance', msg);

  }

}

class Child extends Parent {

  static myMethod(msg) {

    super.myMethod(msg);

  }

  myMethod(msg) {

    super.myMethod(msg);

  }

}

Child.myMethod(1); // static 1

var child = new Child();

child.myMethod(2); // instance 2

答:Child.myMethod(1);这句输出:'static'   1

child.myMethod(2);这句输出:'instance'   2

6、请利用class重新定义Cat,并让它从已有的Animal继承,然后新增一个方法say(),

返回字符串'Hello, xxx!'

class Animal {

    constructor(name) {

        this.name = name;

    }

}

答:class Cat extends Animal{

                            constructor(name,color) {

                                super(name);

                                   this.color = color;

                            }

                            say(){

                                   return `Hello,${this.name}!`;

                            }

                     }

7、.接上面程序分析下面代码执行结果为

var kitty = new Cat('Kitty');

var doraemon = new Cat('哆啦A梦');

if ((new Cat('x') instanceof Animal) && kitty && kitty.name === 'Kitty' && kitty.say &&

 typeof kitty.say === 'function' && kitty.say() === 'Hello,Kitty!'  &&

kitty.say === doraemon.say) {

         console.log('测试通过!');

} else {

        console.log('测试失败!');

}

答:执行结果为:测试通过。

8、下面程序执行结果为

(typeof (new (class { class () {} })));

答:object

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值