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

使用原型链如何实现继承 #589

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

使用原型链如何实现继承 #589

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

Comments

@lgwebdream
Copy link
Owner

No description provided.

@lgwebdream
Copy link
Owner Author

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

@rubickecho
Copy link

function animal() {
    this.name = 'animal';
    this.colors = ['red', 'blue', 'green'];
}

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

function dog() {
}

dog.prototype = new animal();

const dog1 = new dog();
console.log('dog1.name:', dog1.getName()); // animal
console.log('dog1.colors:', dog1.colors); // ['red', 'blue', 'green']

dog1.name = 'dog';
dog1.colors.push('black'); // 引用类型的值会共享
const dog2 = new dog();
console.log('dog2.name:', dog2.getName()); // animal
console.log('dog2.colors:', dog2.colors); // ['red', 'blue', 'green', 'black']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants