title: “Js 原型链&this”
date: 2022-09-01T22:20:32+08:00
原型链图解
两个案例:
一、普通函数的内存图
function fn1(usname,age,height,weight){
var aa = 1;
var bb = 2;
this.usname = usname;
this.age = age;
this.height = height;
this.weight = weight;
return 123;
}
var a = fn1("xh",18,180,140);
二、构造函数的内存图(包含函数的原型和对象原型)
function Person(usname,age,height,weight){
this.usname = usname;
this.age = age;
this.height = height;
this.weight = weight;
}
var a = new Person("xh",18,180,140);
-
函数(这里指案例2中的Person函数)身上有显式原型
prototype
指向函数的原型,函数原型的身上有构造器(constructor
)和隐式原型(__proto__
)。其中,构造器(constructor
)指向该函数(案例2中的Person函数);隐式原型(__proto__
)指向Object
原型对象(所有对象的根原型)。 -
由函数(这里指案例2中的Person函数)实例化的对象(
new Person
)身上有隐式原型(__proto__
),该隐式原型(__proto__
)与该函数(这里指案例2中的Person函数)的显式原型prototype
指向相同。