当前位置:网站首页>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
边栏推荐
- Is it profitable to host an Olympic Games?
- The biggest pain point of traffic management - the resource utilization rate cannot go up
- Nodejs教程之Expressjs一篇文章快速入门
- LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
- Can novices speculate in stocks for 200 yuan? Is the securities account given by qiniu safe?
- Deployment of external server area and dual machine hot standby of firewall Foundation
- OneNote in-depth evaluation: using resources, plug-ins, templates
- 7. Data permission annotation
- Laravel notes - add the function of locking accounts after 5 login failures in user-defined login (improve system security)
- JS get array subscript through array content
猜你喜欢
KDD 2022 | 通过知识增强的提示学习实现统一的对话式推荐
OneNote 深度评测:使用资源、插件、模版
KDD 2022 | realize unified conversational recommendation through knowledge enhanced prompt learning
968 edit distance
[asp.net core] set the format of Web API response data -- formatfilter feature
【滑动窗口】第九届蓝桥杯省赛B组:日志统计
Reviewer dis's whole research direction is not just reviewing my manuscript. What should I do?
Why do job hopping take more than promotion?
OAI 5g nr+usrp b210 installation and construction
After working for 5 years, this experience is left when you reach P7. You have helped your friends get 10 offers
随机推荐
Spark SQL chasing Wife Series (initial understanding)
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
2022 fields Award Announced! The first Korean Xu Long'er was on the list, and four post-80s women won the prize. Ukrainian female mathematicians became the only two women to win the prize in history
How to implement common frameworks
OneNote 深度评测:使用资源、插件、模版
document.write()的用法-写入文本——修改样式、位置控制
【mysql】游标的基本使用
R語言可視化兩個以上的分類(類別)變量之間的關系、使用vcd包中的Mosaic函數創建馬賽克圖( Mosaic plots)、分別可視化兩個、三個、四個分類變量的關系的馬賽克圖
新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
Seven original sins of embedded development
Infrared thermometer based on STM32 single chip microcomputer (with face detection)
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
【Redis设计与实现】第一部分 :Redis数据结构和对象 总结
Variable star --- article module (1)
防火墙基础之外网服务器区部署和双机热备
SAP UI5 框架的 manifest.json
ICML 2022 | Flowformer: 任务通用的线性复杂度Transformer
PG基础篇--逻辑结构管理(事务)
Ravendb starts -- document metadata