javascript has five kinds of objects built base (Fundamental Objects) , , Object
, Function
, Error
, Symbol
, Boolean
and Object
/ Function
is particularly special is the basis for the definition of other built-in objects or objects and ordinary methods.
Detailed understanding Object
and Function
objects contribute to a better understanding of some of the works of javascript.
And other reference types like Object
/ Function
are both objects have their own methods and properties, but also function as a constructor. This article focuses on the following questions:
Fucntion.prototype
And ordinary objectsprototype
What is the difference?Object.prototype.__proto__
= ?Object.__proto__
= ?Object
,Function
What is special about the prototype object?
Function
Function properties
In standard ES6, Function object has two attributes:
-
length value is 1, this property is characteristic
{ [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }
, that is, not covered, can not befor...in
traversed, but canObject.defineProperty
modify its properties above -
prototype prototype object ( see ES latest standard instructions Function.prototpye ) it with the general function of
prototype
that difference- ❗️ it can not write, can not be configured, can not traverse. That is, it always points to a fixed object and other prototype object of all the functions , all the function itself
__proto__
point to it.
const o = {number: 20} Function.prototype = o // 依然是原来的值,未改变 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指向o
- ❗️ it is a function. Function generally
prototype
is having aconstructor
general object properties, butFunction
isprototype
a function object (built-in function object
), isjs
only a defaultprototype
as a function of object
typeof Function.prototype // 'function' function F () {} typeof F.prototype // ☘ 'object' typeof Object.prototype // 'object'
- ❗️ it can not write, can not be configured, can not traverse. That is, it always points to a fixed object and other prototype object of all the functions , all the function itself
This is a requirement of the ES standard Function
two properties of the object, but in fact in FireFox Chrome at the time of implementation, there is one name
attribute, its value is 'Function'
. There is also a property, that is,__proto__
Compared to Object
, Function
the object comes attributes is relatively small
★ Function.prototype
In ES specification, the relevant portion Function.prototype definition Function
of the prototype
method are
Function.prototype.apply
Function.prototype.bind
Function.prototype.call
Function.prototype.contructor
Function.prototype.toString
Function.prototype[@@hasInstance](V)
Functions and objects have __proto__
attributes, point constructor prototype
Object attribute points, i.e., its prototype object.
And the function of the __proto__
attribute (❗️ prototype object which is not prototype
on the __proto__
attribute) point Function.prototype
, so that Function.prototype
the properties and methods that will be the object function ( function Object ) inherited.
Through the above description, I believe that to understand why these less interesting equation holds
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
At the same time, because the function object itself has prototype
properties that Object
instance, it also inherited the Object.prototype
property .
Object
★ Object Properties function object
Object
As a function, and normal function, have length
, prototype
, __proto__
, name
property, in addition, there are still many inherited私有方法
// 方法
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()
Object
Method function object is not the point here, it is no longer expand.
★ Object.prototype
And Function.prototype
other reference types ( Array.prototype
, ) String.prototype
is the same as not write , can not be configured , not for ... in traverse , but still can be extended to the past, Object.prototype
new properties and methods
Object.isExtensible(Object.prototype) // true
- ❗️
Object.prototype
An important feature is that it is the end of all the object prototype chain as aObject.prototype.__proto__
valuenull
, i.e.,
Object.prototype.__proto__ === null
Examples of an object, along its prototype chain, through __proto__
layers up to find a single property, if Object.prototype
not found on, it will return undefined
, so the prototype chain is not unlimited looking down.
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.color -> 没找到,继续
* f.__proto__.color(F.prototype) -> 没找到,继续
* f.__proto__.__proto__.color(F.prototype.__proto__,Object.prototype) 没找到,返回undefined
* 如果继续 f.__proto__.__proto__.__proto__ (Object.prototype.__proto__) === null 结果跟上面一样
*/
console.log(f.color) // undefined
Object.prototype
The properties and methods, will be inherited by all of the methods and objects js, ES specification attributes
Object.prototype.constructor
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toString()
Object.prototype.valueOf()
Object.prototype.__proto__
The figure is Function
, Object
and Function.prototype
, Object.prototye
related to each other in FIG.
Relationship Object, Function of
Object
And Function
most people between blurred, 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
To be continued ~ ~ ~