1,从原型概念上讲述区别
简单说下应用场景吧,
Object.create 目前知道的有两个地方:
{}字面量,,方便初始化创建大量静态键值对,同时可以手动设置__proto__达到更Object.creat类似效果。
const a = { a: 1 };
const b = Object.create(a);
[a, b].forEach(v => {
v.b = "hello";
console.log("--", v, v.a);
console.log("KEYS: ", Reflect.ownKeys(v).join(","));
});
console.log(
"prototype of a is ",
Reflect.getPrototypeOf(a),
", is it Object.prototype? ",
Reflect.getPrototypeOf(a) === Object.prototype
);
console.log(
"prototype of b is ",
Reflect.getPrototypeOf(b),
", is it a? ",
Reflect.getPrototypeOf(b) === a);
-- { a: 1, b: 'hello' } 1
KEYS: a,b
-- { b: 'hello' } 1
KEYS: b
prototype of a is [Object: null prototype] {} , is it Object.prototype? true
prototype of b is { a: 1, b: 'hello' } , is it a? true
13 回答13k 阅读
7 回答2.2k 阅读
3 回答1.3k 阅读✓ 已解决
6 回答1.3k 阅读✓ 已解决
2 回答1.4k 阅读✓ 已解决
3 回答1.3k 阅读✓ 已解决
6 回答1.1k 阅读
字面量继承自Object
Object.create继承自接受的参数