Javascript what is Object vs Object.prototype when used in the Object.create proto parameter

I am trying to understand the difference between Object and Object.prototype. Because Object.prototype is used to create an empty object. I felt why not Object.

I create an object in the following ways.

Method 1:

o = Object.create(Object.prototype,{ p : {value: "test"} });
console.log(o.__proto__);

result:

Object {__defineGetter__: function, __defineSetter__: function, hasOwnProperty: function, __lookupGetter__: function, __lookupSetter__: function…}

and

console.log(o)

result

Object {p: "test"}
    p : "test"
    __proto__ : Object
        constructor : function Object()
        hasOwnProperty : function hasOwnProperty()
        isPrototypeOf : function isPrototypeOf()
        propertyIsEnumerable : function propertyIsEnumerable()
        toLocaleString : function toLocaleString()
        toString : function toString()
        valueOf : function valueOf()
        __defineGetter__ : function __defineGetter__()
        __defineSetter__ : function __defineSetter__()
        __lookupGetter__ : function __lookupGetter__()
        __lookupSetter__ : function __lookupSetter__()
        get __proto__ : function __proto__()
        set __proto__ : function __proto__()

against

o = Object.create(Object,{ p : {value: "test"} });
console.log(o.__proto__);

result:

function Object() { [native code] }

and

console.log(o)

result:

Function {p: "test"}
    p : "test"
    __proto__ : function Object()
        arguments : null
        assign : function assign()
        caller : null
        create : function create()
        defineProperties : function defineProperties()
        defineProperty : function defineProperty()
        entries : function entries()
        freeze : function freeze()
        getOwnPropertyDescriptor : function getOwnPropertyDescriptor()
        getOwnPropertyDescriptors : function getOwnPropertyDescriptors()
        getOwnPropertyNames : function getOwnPropertyNames()
        getOwnPropertySymbols : function getOwnPropertySymbols()
        getPrototypeOf : function getPrototypeOf()
        is : function is()
        isExtensible : function isExtensible()
        isFrozen : function isFrozen()
        isSealed : function isSealed()
        keys : function keys()
        length : 1
        name : "Object"
        preventExtensions : function preventExtensions()
        prototype : Object
        seal : function seal()
        setPrototypeOf : function setPrototypeOf()
        values : function values()
        __proto__ : function ()
        [[FunctionLocation]] : <unknown>

In general, I found that:

o = {};
// is equivalent to:
o = Object.create(Object.prototype);

why not

o = {};
// is equivalent to:
o = Object.create(Object);
+4
source share
1 answer

The reason Objectis the function used to create the objects:

Object instanceof Function

So you can also do:

const o = new Object();

javascript, , new , .prototype, , :

const o = Object.create( Object.prototype );
Object.call( o );

Object.create( Object )

, . , , Object , Function.prototype, Object.prototype...

+1

Source: https://habr.com/ru/post/1687939/


All Articles