JavaScript function advanced study notes (1)

Prototype and Prototype Chain

Prototype

Function prototype property

  1. Each function has a prototype attribute, which by default points to an Object empty object (ie prototype object)
console.log(Date. prototype, typeof Date. prototype )
function fun () {
    
    
}
console.log (fun.prototype)		//默认指向一个空Object对象
  1. There is a property constructor in the prototype object, which points to the function object
console.log(Date.prototype.constructor===Date )
console.log(fun.prototype.constructor===fun)

Add properties to the prototype object

  1. All instance objects of the function automatically have the properties in the prototype
fun.prototype.test = function () {
    
    
	console.log('test()')
}

Explicit and implicit prototypes

Explicit prototype

Every function function has a prototype property, which is an explicit prototype

Implicit prototype

Each instance object has a _ _ proto _ _ property, which is the implicit prototype

Relationship difference

  1. The value of the implicit prototype of the object is the value of the explicit prototype of its corresponding constructor
  2. The explicit prototype is automatically added when the function is defined, and the default value is an empty Object object
  3. The implicit prototype _ _ proto _ _ is automatically added when the instance object is created, and the default value is the prototype property value of the constructor

Prototype chain (implicit prototype chain)

Role:
find the properties of the object (method)

When we are accessing the properties of an object, the search order of the properties:

  1. First search in its own properties, find and return
  2. There is no own property, look up along _ _ proto _ _ (implicit prototype chain), find and return
  3. The prototype chain has reached the end and still not found, return undefined

Reference materials, B station is still Silicon Valley JavaScript advanced tutorial.

Guess you like

Origin blog.csdn.net/qq_43592084/article/details/110677852