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

原型方法,没有 __proto__ 的对象 #46

Open
0xtokobin opened this issue Feb 21, 2023 · 0 comments
Open

原型方法,没有 __proto__ 的对象 #46

0xtokobin opened this issue Feb 21, 2023 · 0 comments
Labels

Comments

@0xtokobin
Copy link
Collaborator

要使用给定的原型创建对象,使用:

字面量语法:{ proto: ... },允许指定多个属性
Object.create(proto, [descriptors]),允许指定属性描述符。
Object.create 提供了一种简单的方式来浅拷贝对象及其所有属性描述符(descriptors)。

let clone = Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));
设置和访问原型的现代方法有:

[Object.getPrototypeOf(obj)]

(https://developer.mozilla.org/zh/docs/Web/JavaScript/Reference/Global_Objects/Object/getPrototypeOf) —— 返回对象 obj 的 [[Prototype]](与 proto 的 getter 相同)。

[Object.setPrototypeOf(obj, proto)]

(https://developer.mozilla.org/zh/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf) —— 将对象 obj 的 [[Prototype]] 设置为 proto(与 proto 的 setter 相同)。

不推荐使用内建的的 proto getter/setter 获取/设置原型,它现在在 ECMA 规范的附录 B 中。

我们还介绍了使用 Object.create(null) 或 {proto: null} 创建的无原型的对象。

这些对象被用作字典,以存储任意(可能是用户生成的)键。

通常,对象会从 Object.prototype 继承内建的方法和 proto getter/setter,会占用相应的键,且可能会导致副作用。原型为 null 时,对象才真正是空的。

@0xtokobin 0xtokobin added the es6 label Feb 21, 2023
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

1 participant