原型链的本质
原型链的本质
class Point {
constructor() {
// ...
}
toString() {
// ...
}
toValue() {
// ...
}
}
// 等同于
Point.prototype = {
constructor() {},
toString() {},
toValue() {},
};
原型链的本质
let object = {age: '123'}
function Parent(name1) {
this.name = name1
}
// 可以得知的是call会把 Parent 挂在到 object 上,在执行一下Object.Parent(),Parent 函数
// 里面的this上的属性就会被挂载到Object上面去。
Parent.call(object, '我奶奶')
console.log('object', object)
// {age: "123", name: "我奶奶"}
原型链的本质
function Parent() {
this.name = ['写代码像蔡徐抻']
}
typeof Parent.prototype
"object"
原型链的本质
function Parent() {
this.name = ['写代码像蔡徐抻']
}
Parent.prototype = {
getName = function() {
return this.name
}
}
new Parent()
{
name: ['写代码像蔡徐抻'],
__proto__: {
getName: function() {
return this.name
}
}
}
原型链的本质
function Parent1() {
console.log('xxxx')
}
Parent1.prototype.constructor() // 也可以
原型链的本质
function aa() {
this.name = 'xxx';
}
let obj = {}
obj.aa = aa;
console.log(obj) // {}
obj.aa()
console.log(obj) // {name: 'xxx'}
// 结论:在哪个作用域执行方法,会把方法内的this属性挂载到当前作用域