当前位置:网站首页>JS学习笔记-OO创建怀疑的对象
JS学习笔记-OO创建怀疑的对象
2022-07-06 12:56:00 【全栈程序员站长】
大家好,又见面了,我是全栈君。
问了、工厂介绍,解决重码
前面已经提到,JS中创建对象的方法。不难发现,主要的创建方法中,创建一个对象还算简单,假设创建多个类似的对象的话就会产生大量反复的代码。
解决:工厂模式方法(加入一个专门创建对象的方法,传入參数避免反复)
function createObject(name,age){
var obj =new Object(); //创建对象
obj.name = name;
obj.age = age;
obj.run = function(){
return this.name + this.age + '处理中...';
};
return obj; //返回对象引用
};问二、引入构造函数,解决对象识别
上面方法尽管攻克了避免反复代码出现的问题。但也带来了无法识别详细对象的问题,方法内部使用new Object的方式,最后返回该对象引用,调用该方法创建的对象返回的所有都是Object的引用。因此使用typeof或instanceof操作符时都无法区分详细对象。
解决:构造函数(改良后的工厂方法)
function Box(name,age){ //创建对象 this.name = name; this.age = age; this.run = function(){ return this.name + this.age + '处理中...'; }; };比較:细心的童鞋就该发现了,该方法与问一中的工厂模式不同之处就在于:省略了newObject()的明文运行过程;省略了return语句,这些都由后台自己主动运行。
而构造函数差别普通函数的地方在于其调用方式,必须用new运算符或对象冒充方式调用。
问三、引入prototype属性对象。解决对象之间的共享问题
每个对象都会有一个prototype,同一时候它也是一个对象。
使用目的是为了解决共享问题,调用同一个构造函数创建的该对象会共享prototype中的属性和方法。
解决:使用原型模式解决共享
function Box() {} //声明一个构造函数 Box.prototype.name = 'Lee'; //在原型里加入属性 Box.prototype.age = 100; Box.prototype.run = function () { //在原型里加入方法 return this.name + this.age + '处理中...'; };比較:
构造函数创建
使用原型创建
细节:在调用属性或方法时,採用就近原则。先查找实例中是否存在,否的话查找原型。可使用isPrototypeOf(),hasOwnPrototy(),in操作符进行相关測试。
问四、使用组合,解决共享及传參
原型模式创建对象省略了构造函数传參初始化的过程,这既是它的缺点又是它的长处,缺点是对象初始化的值一样,而且假设原型属性中包括有引用类型,则对一个对象进行更改。其它对象的相应属性也会跟着更改了。
解决:组合构造函数+原型模式(解决共享和传參的问题)
function Box(name, age) { //不共享的使用构造函数 this.name = name; this.age = age; this. family = ['父亲', '母亲', '妹妹']; }; Box.prototype = { //共享的使用原型模式 constructor : Box, run : function () { return this.name + this.age + this.family; } };细节:这样的方式事实上就是将构造函数与原型一起使用。对要创建的对象分析,将须要共享的内容放入原型中,不须要的则放在构造函数里。这样也就是组合了。
优化:这样分开式的写法难免有些怪异。我们将这两部分合并
动态原型模式(第一次调用共享方法时进行初始化原型。以后就不会初始化了)
function Box(name ,age) { //将全部信息封装到函数体内 this.name = name; this.age = age; if (typeof this.run != 'function') {//仅在第一次调用的初始化 Box.prototype.run = function () { return this.name +this.age + '处理中...'; }; } }中结:
在学习JS中,还是非常须要对正统面向对象语言的理解的,在这里我们学习了使用构造函数以及原型来创建对象,理解了二者的概念,对于后面的JS中面向对象深入学习会非常有帮助。创造出各种不同的方法来解决的不同情况下的问题,了解按需这些人才。
版权声明:本文博主原创文章,博客,未经同意不得转载。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/117101.html原文链接:https://javaforall.cn
边栏推荐
- 039. (2.8) thoughts in the ward
- R語言可視化兩個以上的分類(類別)變量之間的關系、使用vcd包中的Mosaic函數創建馬賽克圖( Mosaic plots)、分別可視化兩個、三個、四個分類變量的關系的馬賽克圖
- El table table - get the row and column you click & the sort of El table and sort change, El table column and sort method & clear sort clearsort
- Le langage r visualise les relations entre plus de deux variables de classification (catégories), crée des plots Mosaiques en utilisant la fonction Mosaic dans le paquet VCD, et visualise les relation
- 请问sql group by 语句问题
- Study notes of grain Mall - phase I: Project Introduction
- Data Lake (VIII): Iceberg data storage format
- The most comprehensive new database in the whole network, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, flying Book Multidimensional table, heipayun, Zhix
- OneNote in-depth evaluation: using resources, plug-ins, templates
- This year, Jianzhi Tencent
猜你喜欢

【mysql】游标的基本使用

【滑动窗口】第九届蓝桥杯省赛B组:日志统计

This year, Jianzhi Tencent

新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀

LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件

967- letter combination of telephone number
![[redis design and implementation] part I: summary of redis data structure and objects](/img/2e/b147aa1e23757519a5d049c88113fe.png)
[redis design and implementation] part I: summary of redis data structure and objects

The difference between break and continue in the for loop -- break completely end the loop & continue terminate this loop

Data Lake (VIII): Iceberg data storage format

Pycharm remote execution
随机推荐
Why do job hopping take more than promotion?
El table table - get the row and column you click & the sort of El table and sort change, El table column and sort method & clear sort clearsort
每个程序员必须掌握的常用英语词汇(建议收藏)
[sliding window] group B of the 9th Landbridge cup provincial tournament: log statistics
代理和反向代理
防火墙基础之外网服务器区部署和双机热备
@Detailed differences among getmapping, @postmapping and @requestmapping, with actual combat code (all)
审稿人dis整个研究方向已经不仅仅是在审我的稿子了怎么办?
Is it safe to open an account in flush? Which securities company is good at opening an account? Low handling charges
Seven original sins of embedded development
Reviewer dis's whole research direction is not just reviewing my manuscript. What should I do?
OSPF多区域配置
Is this the feeling of being spoiled by bytes?
Web开发小妙招:巧用ThreadLocal规避层层传值
Statistical inference: maximum likelihood estimation, Bayesian estimation and variance deviation decomposition
for循环中break与continue的区别——break-完全结束循环 & continue-终止本次循环
快过年了,心也懒了
Web开发小妙招:巧用ThreadLocal规避层层传值
Spiral square PTA
039. (2.8) thoughts in the ward