继承在 ES5-Javascript 中是如何工作的?

How does inheritance work in ES5-Javascript?

对于下面的代码,

           var customer={
                name: "Tom Smith",

                speak: function(){
                    return "My name is " + this['name'];
                },

                address: {
                    street: '123 Main St',
                    city: "Pittsburgh",
                    state: "PA"
                }
            };

下面是我对客户对象的可视化,


我的问题:

customer 对象仅继承 Object.prototype 的属性(内置)吗?

Object函数类型对象的属性(内置)也是为了继承吗?

如果是,customer对象继承Object属性的语法是什么?

Below is my visualisation of customer object

如果你使用术语 [[prototype]] 而不是 __proto__ 会更好 - 你可以看,.__proto__ 只是 getter/setter 继承自 Object.prototype

Does customer object inherit properties(built-ins) of Object.prototype only?

是的。尽管你可以 add your own properties to Object.prototype,它们也会被继承,但不仅仅是内置的。

Are the properties(built-ins) of Object are also for the purpose of inheritance?

没有。它们是静态函数,应该以对象作为参数调用,而不是对象方法。

What is the syntax for customer object to inherit Object properties?

Object 是一个函数,通常您 不想 从它继承。如果你真的想要,你可以使用 Object.create(Object).

此外,ES6 添加了一种新方法来实现这一点,因为 classes 也从它们的父级继承静态方法:

class MyObject extends Object {
    static myCreate(x) {
        return this.create(x);
    }
}