xinbaihui / Blog

web front end
1 stars 0 forks source link

【prototype】What's the output? #11

Open xinbaihui opened 5 years ago

xinbaihui commented 5 years ago
function Person(firstName, lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
}

const member = new Person("Lydia", "Hallie");
Person.getFullName = function() {
  return `${this.firstName} ${this.lastName}`;
};

console.log(member.getFullName());

A: TypeError B: SyntaxError C: Lydia Hallie D: undefined undefined

Answer A

解析: new Person()做了什么

const obj = {}
Person.call(obj, para)
obj.__proto__ = Person.prototype
return obj

思考:

参考:https://github.com/lydiahallie/javascript-questions, 11