<> prototype

The prototype is function Object , It defines the common ancestor of constructors , It's a parent-child relationship , The child object inherits the methods and properties of the parent object

* prototype Is the property under the function , Object wants to see the prototype using implicit properties __Proto__
* constructor Point to constructor
* You have attributes in yourself , There are attributes in prototypes , Close , Use your own
By adding properties to the prototype , All instantiated objects can share properties and methods
Car.prototype = { height : 1400, lang : 4900, carName : 'BMW' } function Car()
{ } var car = new Car();
<> Prototype chain

Each instance object has __proto__ attribute , By attributes __proto__ A prototype object that points to a constructor , When it reaches the end , return null, Look up to the top layer by layer , It forms a prototype chain

prototype It's function specific ,__proto__ Yes, there are ,js All things in the world are objects

<>prototype and ——proto—— Differences and functions

prototype Define common attributes in advance , Use for later objects

prototype The existence of inheritance realizes inheritance , Save memory space

__proto__ It's about the object ,prototype It's a function , Because functions are also objects , So there are functions __proto__;

__proto__ When accessing the properties of an object , If this property does not exist inside the object , Then it will follow its path **__proto__
** The object that the property points to ( parent object ) Look for it in the library , That's the prototype chain

prototype The function is to make the objects instantiated by the function can find common properties and methods

The significance of object prototype is to provide a direction for object search mechanism , Or a route , But it's a nonstandard property , Therefore, in the actual development , This property cannot be used , It just points internally to the prototype object

<>constructor Constructors

constructor Property exists in __proto__ and prototype, It points to the constructor itself

Normally , Object is set in the prototype object of the constructor . If there are multiple object methods , We can assign values to prototype objects in the form of objects , But this will override the original contents of the constructor prototype object , So the modified prototype object
constructor No longer points to the current constructor . here , We can use it in the modified prototype object , Add one constructor Points to the original constructor .

problem Modified the prototype object of the function ,constructor Who's the point of this
function Star(uname, age) { this.uname = uname; this.age = age; } //
In many cases , We need to use it manually constructor This property refers back to The original constructor Star.prototype = { //
If we modify the original prototype object , An object is assigned to a prototype object , It must be used manually constructor Returns the original constructor constructor: Star, //
Manually set the pointer back to the original constructor sing: function() { console.log(' I can sing '); }, movie: function() {
console.log(' I'll be in a movie '); } } var zxy = new Star(' Zhang Xueyou ', 19); console.log(zxy)
When modifying function prototypes , because Star.prototype It's an object , therefore constructor Point to the prototype that constructs this object , that is object


adopt call``apply It can be changed this The direction of , Borrow other people's functions to complete their own functions

difference :call Transfer multiple parameters apply Pass an array of parameters
function Person(name,age,sex) { this.name = name; this.age = age; this.sex =
sex; } function Student(name,age,sex,tel,grade) { //var this = {name: "lin",
age: "19", sex: "male", tel: 123, grade: 78} Person.call(this,name,age,sex);
// adopt call change this Point to this function //Person.apply(this,[name,age,sex]) this.tel = tel; this.
grade= grade; } var student = new Student('lin','19','male',123,78);

* Create an empty object
* Constructor this, Inheritance function prototype
* Give Way this Object instance pointing to constructor , Execute the contents of the constructor to add properties and methods to the new object
* return this var obj = {}// Create an empty object obj.__proto__ = Person.prototype;// Inheritance scope Person.
call(obj,)// change this point // These three steps are implicit var person = new Person();//new operation