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

继承 #93

Open
lovelmh13 opened this issue Jun 28, 2021 · 0 comments
Open

继承 #93

lovelmh13 opened this issue Jun 28, 2021 · 0 comments

Comments

@lovelmh13
Copy link
Owner

// 寄生组合继承
function Father(name) {
  this.name = name
  this.city = '北京'
}

Father.prototype.getName = function () {
  return this.name
}

function Child(name, age) {
  Father.call(this, name) // 继承属性
  this.age = age
}

// 继承方法 等价于 Child.prototype.__proto__ = Father.prototype
Child.prototype = Object.create(Father.prototype) // 原型链
Child.prototype.constructor = Child // 原型链,注意这里的构造函数是指回 Child

Child.prototype.getAge = function () {
  return this.age
}

var child = new Child('lmh', 25)
var myname = child.getName()
var age = child.getAge()

console.log(myname)
console.log(age)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant