Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

已知函数 A,要求构造⼀个函数 B 继承 A #333

Open
lgwebdream opened this issue Jul 6, 2020 · 2 comments
Open

已知函数 A,要求构造⼀个函数 B 继承 A #333

lgwebdream opened this issue Jul 6, 2020 · 2 comments
Labels
JavaScript teach_tag 新东方 company 编程题 teach_tag

Comments

@lgwebdream
Copy link
Owner

function A(name) {
  this.name = name;
}
A.prototype.getName = function () {
  console.log(this.name);
};
@lgwebdream lgwebdream added JavaScript teach_tag 新东方 company 编程题 teach_tag labels Jul 6, 2020
@lgwebdream
Copy link
Owner Author

扫描下方二维码,获取答案以及详细解析,同时可解锁800+道前端面试题。

@GolderBrother
Copy link

// 一: ES5版本
function A(name) {
  this.name = name;
}
A.prototype.getName = function () {
  console.log(this.name);
};

function B(name, age){
	A.call(this, name);
	this.age = age;
}
B.prototype = Object.create(A.prototype);
B.prototype.constructor = B;
B.prototype.getAge = function(){
	console.log(this.age);
}

const b = new B('james', 18);
b.getName();
b.getAge();
console.log(b);

// 二: ES6版本
function A(name) {
  this.name = name;
}
A.prototype.getName = function () {
  console.log(this.name);
};
class B extends A{
	constructor(name, age){
		super(name);
		this.age = age;
	}
	getAge(){
		console.log(this.age);
	}
}
const b = new B('james', 18);
b.getName();
b.getAge();
console.log(b);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
JavaScript teach_tag 新东方 company 编程题 teach_tag
Projects
None yet
Development

No branches or pull requests

2 participants