Javascript has five Fundamental Objects: Object, Function, Error, Symbol, and Boolean. Object/Function is especially special. Is the basis for defining other built-in objects or ordinary objects and methods.

A detailed understanding of Object and Function objects will help you better understand some of how javascript works.

Like other reference types, Object/Function is both an Object with its own methods and properties, and a Function that can be used as a constructor. This paper mainly discusses the following issues:

  • Fucntion.prototypeAnd ordinary objectsprototypeWhat’s the difference?
  • Object.prototype.__proto__=?
  • Object.__proto__=?
  • Object,FunctionWhat’s special about the prototype object?

Function

The Function of the attribute

In the ES6 standard, a Function object has two properties:

  • {[[Writable]]: false, [[Enumerable]]: false, [[Writable]]: true}, [[64x]: 64x}, disables any additional control system that works without any additional control. In traversal, but you can modify its above properties via Object.defineProperty

  • Prototpye is a prototype object. Prototpye is a prototype object. Prototpye is a prototype object. Prototpye is a prototype object

    • ❗️ It cannot be written, configured, or traversed.That is, it always points to a fixed object and something elseThe prototype object for all functionsOf all the functions themselves__proto__Pointing to it.
    const o = {number: 20}
    Function.prototype = o // The value remains unchanged
    
    
    typeof Array.__proto__ // 'function' [=== Function.prototype]
    typeof Object.__proto__ // 'function' [=== Function.prototype]
    
    typeof Array.prototype.__proto__ // 'object' [=== Object.prototype]
    
    function F () {}
    F.__proto__ === Function.prototype // true
    
    F.prototype = o     // prototype points to o
    Copy the code
    • ❗️ it is a function.General functionalprototypeIt’s the one with theconstructorProperty of a normal object, butFunctiontheprototypeIs a function object (built-in function object),isjsThe only default inprototypeIs the object of a function
    typeof Function.prototype // 'function'
    
    function F () {}
    typeof F.prototype  / / ☘ 'object'
    typeof Object.prototype // 'object'
    Copy the code

These are the two properties of the Function object specified in the ES standard, but there is also a name property in FireFox and Chrome, which has a value of ‘Function’. There is another attribute, __proto__

Compared with Object, Function objects come with fewer attributes

To end the Function prototype

In the ES specification, the prototype methods for Function defined in the function. prototype section are

Function.prototype.apply
Function.prototype.bind
Function.prototype.call
Function.prototype.contructor
Function.prototype.toString
Function.prototype[@@hasInstance](V)
Copy the code

Both functions and objects have a __proto__ attribute that points to the object that the constructor’s prototype attribute points to, its prototype object.

The Function’s __proto__ attribute (❗️ is not the __proto__ attribute on its prototype object) points to function. prototype, So function. prototype properties and methods are inherited by Function objects.

Through the above introduction, I believe that we can understand the following interesting equations why

Function.__proto__ === Function.prototype / / true ❗ ️
Object.__proto__ === Function.prototype // true
Object.prototype.__proto__ === null // true
Function.prototype.__proto__ === Object.prototype // true
Object.prototype === Object.__proto__ // false
Copy the code

At the same time, because the function Object itself has the prototype property and is an instance of Object, it also inherits the Object. Prototype property.

Object

★ Object Function Attributes of an Object

Object, as a function, has the same length, prototype, __proto__, name attributes as normal functions. In addition, there are many private methods that are not inherited

// Method Object.assign() object.create () Object.defineProperties() Object.defineProperty() object.entries () object.freeze ()  Object.getOwnPropertyDescriptor() Object.getOwnPropertyDescriptors() Object.getOwnPropertyNames() Object.getOwnPropertySymbols() Object.getPrototypeOf() Object.is() Object.isExtensible() Object.isFrozen() Object.isSealed() Object.keys() Object.preventExtensions() Object.seal Object.setPrototypeOf() Object.values()Copy the code

The Object method is not the focus here, so it will not be expanded.

Taken the Object. The prototype

Prototype and other reference types (array. prototype, string. prototype) are writable, configurable, and not for… Object. Prototype new properties and methods can still be extended

Object.isExtensible(Object.prototype) // true
Copy the code
  • ❗ ️Object.prototypeAn important property of the object is that it is the end of the prototype chain for all objects becauseObject.prototype.__proto__The value ofnull, i.e.,
Object.prototype.__proto__ === null
Copy the code

An instance of an Object moves up its prototype chain through __proto__ hierarchy to find a property. If not found on Object.prototype, undefined is returned, so the prototype chain is not infinite.

function F () {}
F.prototype.age = 20
let f = new F()
f.__proto__ === F.prototype // true
f.__proto__.__proto__ === Object.prototype //true
f.__proto__.proto__.__proto__ === null // true

* f.__proto__. Color (f.prototype) -> not found, __proto__. Color (f.prototype.__proto__, Object. Prototype) __proto__.__proto__ (object.prototype. __proto__) === null returns the same */ as above
console.log(f.color) // undefined
Copy the code

Object. Prototype properties and methods will be inherited by all methods and objects in JS, ES specification properties

Object.prototype.constructor
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toString()
Object.prototype.valueOf()
Object.prototype.__proto__
Copy the code

Function. Prototype Object. Prototye

Object and Function

The most puzzling thing between Object and Function is their relationship

Object instanceof Object // true
Object instanceof Function // true
Function instanceof Function // true
Function instanceof Object // true

const o = {}
o instanceof Object //true
o instanceof Function // false

function F () {}
F instanceof Object //true
F instanceof Function //true
Copy the code

To be continued