题目1
function Person(name) {
this.name = name;
}
Person.prototype.greet = function() {
console.log('Hello, my name is ' + this.name);
};
function Student(name, grade) {
Person.call(this, name);
this.grade = grade;
}
Student.prototype = Object.create(Person.prototype);
Student.prototype.constructor = Student;
var john = new Student('John', 'Grade 10');
john.greet();
请问上述代码的输出结果是什么?
答案1
输出结果是:Hello, my name is John
题目2
function Animal(name) {
this.name = name;
}
Animal.prototype.sound = function() {
console.log(this.name + ' makes a sound.');
};
function Dog(name, breed) {
Animal.call(this, name);
this.breed = breed;
}
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
var sparky = new Dog('Sparky', 'Golden Retriever');
sparky.sound();
请问上述代码的输出结果是什么?
答案2
输出结果是:Sparky makes a sound.
题目3
function Vehicle(type) {
this.type = type;
}
Vehicle.prototype.drive = function() {
console.log('This ' + this.type + ' is driving.');
};
function Car(type, model) {
Vehicle.call(this, type);
this.model = model;
}
Car.prototype = Object.create(Vehicle.prototype);
Car.prototype.constructor = Car;
var myCar = new Car('car', 'Toyota');
myCar.drive();
请问上述代码的输出结果是什么?
答案3
输出结果是:This car is driving.
题目4
function Fruit(name) {
this.name = name;
}
Fruit.prototype.describe = function() {
console.log('This is a ' + this.name);
};
function Apple(name, color) {
Fruit.call(this, name);
this.color = color;
}
Apple.prototype = Object.create(Fruit.prototype);
Apple.prototype.constructor = Apple;
var greenApple = new Apple('Green Apple', 'green');
greenApple.describe();
请问上述代码的输出结果是什么?
答案4
输出结果是:This is a Green Apple
题目5
function Shape() {
this.type = 'shape';
}
Shape.prototype.getType = function() {
return 'Type: ' + this.type;
};
function Triangle() {
Shape.call(this);
this.type = 'triangle';
}
Triangle.prototype = Object.create(Shape.prototype);
Triangle.prototype.constructor = Triangle;
var myTriangle = new Triangle();
console.log(myTriangle.getType());
请问上述代码的输出结果是什么?
答案5
输出结果是:Type: triangle