当前位置:网站首页>js 构造函数 return 非空对象,其实例化的对象在原型上的差异
js 构造函数 return 非空对象,其实例化的对象在原型上的差异
2022-07-30 12:07:00 【ymzhaoUSTB】
一、构造函数
先看下构造函数的定义
ECMAScript 中的构造函数是用于创建特定类型对象的。构造函数也是函数,只是使用 new 操作符调用构造函数会执行如下操作:
(1) 在内存中创建一个新对象
(2) 这个新对象内部的 [[Prototype]] 特性被赋值为构造函数的 prototype 属性
(3) 构造函数内部的 this 被赋值为这个新对象(即 this 指向新对象)
(4) 执行构造函数内部的代码(给新对象添加属性)
(5) 如果构造函数返回非空对象,则返回该对象;否则,返回刚创建的新对象
二、分析
如果不清楚构造函数的定义,就没有必要看这部分了
根据定义,这里将构造函数分为两种:有无返回非空对象。具体区别在哪儿,之前从未细想过
示例1:实例在原型上的不同
function Foo(name) {
this.name = name
}
function Bar(name) {
this.age = 27
return {
name }
}
let foo = new Foo('Tom'), bar = new Bar('Tom')
console.log(foo.__proto__ === Foo.prototype) // true
console.log(bar.__proto__ === Bar.prototype) // false

上图中也可以看出,foo的原型指向Foo的原型,而bar的原型指向Object的原型
示例2
通过这个例子更明显的对比构造函数中的 this 与 return 的实例
function Foo(name) {
const _this = this
this.name = name
function test(){
console.log(this, _this)
}
return {
name: 'Tom',
test
}
}
let foo = new Foo('Anne')
console.log(foo)
foo.test()
执行 foo.test()this 打印的是foo,即,实例化时return出来的对象_this 打印的是实例化过程中创建的新对象,根据定义,该对象原型指向构造函数的原型。相比之下,前者是对象字面量,其原型指向Object的原型

原型上的区别,意味着,上例中,foo的原型也不指向 Foo的原型,原型的构造函数也并非 Foo。
因此,foo无法访问Foo的原型属性与方法:
function Foo(name) {
const _this = this
this.name = name
function test(){
return _this
}
return {
name: 'Tom',
test
}
}
let foo = new Foo('Anne'), _this = foo.test()
Foo.prototype.age = 18
Foo.prototype.func = function() {
console.log(this.name, this.age)
}
console.log(!!foo.func, !!_this.func) // false true
_this.func() // Anne 18
针对这点,可以在构造函数内手动将返回的对象原型指向构造函数的原型
如果用不上原型链的话,可以直接忽视这点
function Foo(name) {
let res = {
}
res.__proto__ = Foo.prototype
return Object.assign(res, {
name })
}
let foo = new Foo('Anne')
Foo.prototype.age = 20
console.log(foo.age) // 20
边栏推荐
猜你喜欢

Add the device library after Vivado installation

云主机上的MongoDB被威胁,开启AUTH认证

C# 枚举类型 于xaml 中区别

反转链表-迭代反转法
![[SCTF2019]Flag Shop](/img/26/20e21ec873f41f2633703216453a44.png)
[SCTF2019]Flag Shop

Horizontal comparison of 5 commonly used registration centers, whether it is used for interviews or technical selection, is very helpful

EXCEL解决问题:如何查找目标区域,是否包含指定字符串?

Verilog grammar basics HDL Bits training 07

北上广线下活动丨年底最不可错过的技术聚会都齐了

Summary of text alignment, line height, space, etc.
随机推荐
[ASP.NET Core] Dependency Injection for Option Classes
企业如何成功完成云迁移?
Zhou Hongyi: Microsoft copied the 360 security model and became the largest security company in the United States
Add the device library after Vivado installation
打破原则引入SQL,MongoDB到底想要干啥???
13-GuliMall Basics Summary
Beijing, Shanghai and Guangzhou offline events丨The most unmissable technology gatherings at the end of the year are all gathered
京东二面痛遭中间件虐杀,30天学透这套中间件小册,挺进阿里
电脑奔溃的时候,到底发生了什么?
概率论得学习整理--番外3:二项式定理和 二项式系数
contentDocument contentWindow,canvas 、svg,iframe
最基础01/完全背包
PyQt5快速开发与实战 8.2 绘图 && 8.3 QSS的UI美化
历时两月,终拿字节跳动offer,算法面试题分享「带答案」
OneNote如何修改已有的笔记本为默认的快速笔记?
从“校园贷”到“直播带货”,追风少年罗敏一直行走在风口浪尖
我又造了个轮子:GrpcGateway
[BJDCTF2020]Cookie is so stable-1|SSTI注入
mapbox-gl开发教程(十四):画圆技巧
C# 时间戳与时间的互相转换