JavaScript对象的创建模式与继承模式示例讲解

作者:花铛 时间:2024-04-23 09:27:40 

对象的创建模式

Object 构造函数模式:先创建空对象,再动态添加属性和方法。

适用场景:初始时对象内部数据不确定。

存在问题:语句太多(这个问题可以通过使用对象字面量模式来解决)。

var person = new Object()
person.name = 'Tom'
person.setName = function(name){
     this.name = name
}

使用对象字面量模式:使用 {} 创建对象,同时指定属性和方法。

适用场景:初始时对象内部数据是确定的。

存在问题:如果创建多个对象,有很多重复代码(这个问题可以通过使用工厂模式来解决)。

var person={
   name:'Tom',
   setName: function (name){
         this.name = name
   }
}

工厂模式:通过工厂函数动态创建对象并返回。

适用场景:需要创建多个对象。

存在问题:对象没有具体的类型(这个问题可以通过使用自定义构造函数模式来解决)。

function createPerson(name) {
var obj = {
name: name,
setName: function (name) {
this.name = name
}
}
return obj
}
var p1 = createPerson('Tom')
var p2 = createPerson('Mary')

自定义构造函数模式:自定义构造函数,通过 new 创建对象。

适用场景:需要创建多个类型确定的对象。

存在问题:每个对象都有相同的数据(方法),浪费内存(这个问题可以通过使用构造函数 + 原型的组合模式来解决)。

function Person(name) {
this.name = name
this.setName = function(name) {
this.name = name
}
}
var p1 = new Person('Tom')
var p2 = new Person('Mary')

构造函数 + 原型的组合模式:自定义构造函数,属性在函数中初始化,方法添加到原型上。

适用场景:需要创建多个类型确定的对象。

function Person(name) {
this.name = name
}
Person.prototype.setName = function(name) {
this.name = name
}
var p1 = new Person('Tom')
var p2 = new Person('Mary')

对象的继承模式

原型链继承:

变量查找作用域链;对象的属性和方法查找原型链。

child.toString()

child.__proto__ = Child.prototype ----> Child.prototype = new Object() ----> {}.__proto__ = Object.prptotype child 能够访问到 toString 方法:是因为 child 实例能够访问到其原型对象上的方法;Child 构造函数的原型对象又指向 Object 构造函数的实例;{} 实例能够访问到其原型对象上的方法,因此顺着原型链 child 能够访问到 Object 构造函数原型对象上的方法。

// 父类型
function Parent() {
this.parentProp = 'parent prop'
}
Parent.prototype.showParent = function() {
console.log(this.parentProp)
}
// 子类型
function Child() {
this.childProp = 'child prop'
}
// 让子类构造函数的 prototype 指向父类的实例对象,就可以形成子类 --- 父类的一条原型链,子类就可以访问到父类原型上的属性和方法。
Child.prototype = new Parent()
// 如果此时 console.log(child.constructor) 会打印输出 Parent。
// 默认情况下,所有的原型对象都会自动获得一个 constructor 属性,指向原型对象所在的函数。因此 child 本身是没有 constructor 属性的,而是在它的原型对象身上;又因为它的原型对象指向了 Parent 的实例对象;Parent 实例对象的原型对象的 constructor 是 Parent,因此 child.constructor = Parent。这样是错误的。
Child.prototype.constructor = Child
Child.prototype.showChild = function() {
console.log(this.childProp)
}
var child = new Child()
child.showParent()

借用构造函数继承:其实并没有真的实现继承,而是在子类构造函数中通过 call() 调用了父类的构造函数。

function Parent(name, age) {
this.name = name
this.age = age
}
function Child(name, age, price) {
Parent.call(this, name, age) // 相当于执行 this.Person(name, age),也就是相当于 this.name = name; this.age = age
this.price = price
}
const child = new Child('Tom', 20, 500)
console.log(child.name, child.age, child.price)

组合继承:利用原型链继承实现对父类型的方法的继承;借用构造函数继承初始化相同的属性(真正在用的是这种方法)。

call() 是继承属性,重写原型是继承方法。

function Parent(name, age) {
this.name = name
this.age = age
}
Parent.prototype.setName = function(name) {
this.name = name
}
function Child(name, age, price) {
   // 初始化相同的属性
Parent.call(this, name, age)
this.price = price
}
// 继承父类的原型对象上的方法
Child.prototype = new Parent()
// 修正 constructor 属性
Child.prototype.constructor = Child
Parent.prototype.setPrice = function(price) {
this.price = price
}
const child = new Child('Tom', 20, 500)
console.log(child.name, child.age, child.price)

来源:https://blog.csdn.net/wsln_123456/article/details/123256735

标签:JS,对象,创建模式,继承模式
0
投稿

猜你喜欢

  • Python实现翻转数组功能示例

    2022-02-28 09:03:09
  • MySQL8.0.21.0社区版安装教程(图文详解)

    2024-01-20 06:33:58
  • GO语言make()分配用法实例

    2024-01-30 00:54:14
  • 利用Python抢回在蚂蚁森林逝去的能量(实现代码)

    2022-07-01 15:15:39
  • SQL Server视图管理中的四个限制条件

    2009-03-06 14:24:00
  • Python实现OCR识别之pytesseract案例详解

    2021-03-02 02:14:35
  • 解决pyinstaller打包exe文件出现命令窗口一闪而过的问题

    2021-09-28 01:03:45
  • python实现定时发送邮件

    2022-12-20 07:49:49
  • Python实现图片添加文字

    2021-11-22 21:17:11
  • Django中的文件的上传的几种方式

    2023-08-26 22:48:51
  • 在任意字符集下正常显示网页的方法二(续)

    2023-11-22 17:36:14
  • CSS 表格元素内容的定位 0

    2008-08-01 17:31:00
  • SQL Server导出表到EXCEL文件的存储过程

    2009-01-06 11:24:00
  • Python实现aes加密解密多种方法解析

    2021-05-13 01:36:53
  • Spring + mybatis + mysql使用事物的几种方法总结

    2024-01-22 02:01:25
  • 详细介绍Ruby中的正则表达式

    2023-12-03 19:44:46
  • Python基础语言学习笔记总结(精华)

    2023-08-26 21:37:23
  • python的继承详解

    2022-12-09 14:53:10
  • 解决pytorch-gpu 安装失败的记录

    2023-04-04 05:44:44
  • 什么是XSLT,什么是XPath

    2008-01-21 13:12:00
  • asp之家 网络编程 m.aspxhome.com