Constructor (prototype)

function Circle(radius){  // By convention, the constructor name begins with a capital letter
	this.radius = radius
}
Circle.prototype.getLength = function(){
	return this.radius * 2 * Math.PI
}
Circle.prototype.getArea = function(){
	return Math.pow(this.radius,2) * Math.PI 
}
let c1 = new Circle(5)  // Call the constructor with the new operator to create the object
Copy the code

New Circle() does four things automatically

  1. Automatically create empty objects
  2. Automatically associates the empty object with a prototype address of square.prototype, which is the prototype of this constructor
  3. The constructor automatically runs an empty object as this keyword
  4. Automatic return this

Constructor Square

  • The Square function itself is responsible for adding attributes to the object itself
  • The square. prototype object is responsible for holding the common attributes of the object

ES6 class grammar

class Circle{
  constructor(radius){
    this.radius = radius
  } 
  getArea(){ 
    return Math.pow(this.radius,2) * Math.PI  
  }
  getLength(){
    return this.radius * 2 * Math.PI
  }
}
let circle = new Circle(5)
console.log(circle.radius)
console.log(circle.getArea())
console.log(circle.getLength())
Copy the code

An aside — about code specifications

case

  • All constructors (functions specifically used to create objects) begin with a capital letter
  • All constructed objects begin with a lowercase letter

The part of speech

  • The function after new uses the noun form asNew Person(), New Object()
  • Other functions are usually preceded by verbs such asCreateSquare (5), the createElement method (' div ')
  • The other rules will come later


Prototype formula (the only formula in JS)

regular

  • Let obj = new Object() is object.prototype
  • Let arr = new Array() is array.prototype
  • Let square = new Square() prototype is square.prototype
  • Let fn = new Function()

conclusion

Your prototype is the object whose Prototype property corresponds to whoever constructed it

Prototype formula

Object.__ proto __ === constructor. Prototype

Answering questions

let x = {}

Excuse me:

  1. What was the prototype of X?

    x.__proto__

    namelyObject.prototypeObject. PrototypeThe corresponding object)

    What is the value of x.__ proto __?

    Object.prototypePrototype = object.prototypeThe corresponding object)

    3. Are the above two questions equivalent?

    equivalent

    4. Please draw all properties of X in memory

Ps: About the attribute name and value

In the figure, window.Object is the Object property name #101, which is the Object property value

let square = new Square(5)

Excuse me:

  1. What was the prototype for Square?

    square.__ proto __

    namelySquare.prototypePrototype = SquareThe corresponding object)

    2. What is the value of square.__ proto __?

Square. Prototype (Square. Prototype)

Object. Prototype

  1. Object. Prototype is constructed by what function? It was born, not made up
  2. Object. Prototype What is the prototype? There is no prototype
  3. Object. The prototype. __ proto __? null





Object classification

Do objects need to be classified

Need to be

One reason for

There are a lot of objects that have the same properties and behavior and you need to put them into the same class like square1 and square2 so that when you create similar objects it’s very convenient

Reason two

However, there are many objects that have other properties and behaviors, so different categories are required. For example, Square, Circle, Rect are different categories, Array, Function are different categories. The objects created by Object are the least characteristic objects

Type v class

type

Type is the classification of JS data, there are seven kinds of four bases, two empty objects

class

Classes are classes for objects. There are numerous common ones, such as Array, Function, Date, RegExp, and so on

The array object

Define an array

Let arr = [1,2,3] let arr = new Array(1,2,3

The array object’s own property

‘0’/’1’/’2’/’length’ Note that attribute names do not have numbers, only strings

Common properties of array objects

'push' / 'pop' / 'shift' / 'unshift' / 'join'

The function object

Define a function

function fn(x,y){return x+y} let fn2 = function fn(x,y){return x+y} let fn = (x,y) => x+y let fn = new Function(‘x’,’y’, ‘return x+y’)

Properties of function objects themselves

'name' / 'length'

Function objects share attributes

'call' / 'apply' / 'bind'

JS Ultimate question

Who constructs window

**Window** can be seen from the constructor property

Who constructs window.object

Function** because all functions are constructed with window.function

Who constructs window.function

** window.function ** Because all functions are themselves constructed by window.function? No, it’s “God’s” arrangement. The browser constructs Function and then specifies that its constructor is itself

test

  1. The true thing about prototypes is that
  • “Prototype of X” is equivalent to “object referred to by X.__ proto __”. Sometimes we can think of “prototype of X” as equivalent to “X.__ proto __” for convenience.
  • The prototype of an object refers to the collection of public attributes of the object and other objects of the same class. For example, obj1 and ob2 both have toString/valueOf. Then the object composed of toString/valueOf attributes is the prototype of obj1 and Obj2. The address of the prototype is normally stored in the constructor’s prototype
  • X.__proto__ and Object. Prototype store the address of the same Object, which is the prototype of X
  • Prototype Every Object has a prototype, except for the “root Object. Prototype”, where the prototype is null
  1. The prototype attribute is correct
  • All functions are born with a Prototype attribute (except arrow functions)
  • All Prototypes are born with a constructor attribute
  • All constructor attributes store the address of the corresponding function as soon as they are born
  • If a function is not a constructor, it still has the Prototype property, but that property is not currently useful. Right
  • If an object is not a function, it will generally have no prototype property, but it will always have a __ proto __ property
  1. The new X() operation does a lot of things for us automatically, including
  • Automatically creates an empty object
  • Automatically point the prototype of the empty object to x. prototype (copy the address saved by X.prototype into the empty object.__ proto __)
  • The empty object is automatically run as this
  • Automatic return this
  1. Window. Object is a Function Object, so the constructor of this “Function Object” is Function

  2. Function is a Function object, so the constructor of this “Function object” is Function

  3. If window.Object is a Function Object, then this “Function Object” __ proto __ is function. prototype

  4. Function is a Function object, so this “Function object” __ proto __ is function. prototype

  5. Object. Prototype is correct

  • Object. Prototye is the prototype of obj.__ proto __ === Object.prototype
  • Object.__ proto __ === Function. Prototype = Function
  • Object.prototye is not the prototype of Object, object. __ proto __ is the prototype of Object
  1. The “constructor” of all “Function objects” is Function