1、画出 object person student原型链关系图
2.编写程序使用ES6定义 Person类,包括类实例属性(name,age),实例方法say()该方法
返回name和age字符串
class Person{
constructor(name,age){
this.name=name;
this.age=age;
this.say=function(){
console.log('姓名'+this.name+' '+'年龄'+this.age);
}
}
}
var p=new Person('张三','28');
p.say();
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');
不对 使用this之前调用super
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
6.请利用class重新定义Cat,并让它从已有的Animal继承,然后新增一个方法say(),
返回字符串’Hello, xxx!’
class Animal {
constructor(name) {
this.name = name;
}
}
class Cat extends Animal{
constructor(name){
super(name)
}
say(){
console.log('hello,xxx!')
}
}
var p=new Cat();
p.say();
Cat.say;
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('测试失败!');
}
测试通过
7.下面程序执行结果为
(typeof (new (class { class () {} })));