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

对象的 prototype(原型) 是什么? #252

Open
Sogrey opened this issue Aug 27, 2020 · 0 comments
Open

对象的 prototype(原型) 是什么? #252

Sogrey opened this issue Aug 27, 2020 · 0 comments

Comments

@Sogrey
Copy link
Owner

Sogrey commented Aug 27, 2020

简单地说,原型就是对象的蓝图。如果它存在当前对象中,则将其用作属性和方法的回退。它是在对象之间共享属性和功能的方法,这也是JavaScript实现继承的核心。

const o = {};
console.log(o.toString()); // logs [object Object] 

即使o对象中不存在o.toString方法,它也不会引发错误,而是返回字符串[object Object]。当对象中不存在属性时,它将查看其原型,如果仍然不存在,则将其查找到原型的原型,依此类推,直到在原型链中找到具有相同属性的属性为止。原型链的末尾是Object.prototype

console.log(o.toString === Object.prototype.toString); // logs true
@Sogrey Sogrey added this to javascript in Web面经题 Aug 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Web面经题
javascript
Development

No branches or pull requests

1 participant