js的封装、继承与多态
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>js的封装、继承与多态</title> </head> <body> <script> window.onload = function() { // 封装 var Book = function(id, name, price) { // 私有变量(在函数内部定义,函数外部访问不到,实例化之后实例化的对象访问不到) var num = 1 var id = id function privateFunction() { console.log('this is private') } // protected(可以访问到函数内部的私有属性和私有方法,在实例化之后就可以对实例化的类进行初始化拿到函数的私有属性) this.getNum = function() { console.log(num) } this.getFunction = function() { privateFunction() } //public(实例化的之后,实例化的对象就可以访问到了~) this.name = name this.copy = function() { console.log('this is public') } } //在Book的原型上添加的方法实例化之后可以被实例化对象继承 Book.prototype.proFunction = function() { console.log('this is proFunction') } //在函数外部通过.语法创建的属性和方法,只能通过该类访问,实例化对象访问不到 Book.setTime = function() { console.log('this is new time') } var book1 = new Book('B11', '悲惨世界', '$99') // 通过this创建的公共属性和方法,实例化的时候会复制一遍,所以可以访问到 console.log(book1.name) book1.copy() // 通过protected的getNum来访问Book的私有变量 book1.getNum() book1.getFunction() // 直接通过实例来访问私有变量是无法访问的 // console.log(book1.num) // book1.privateFunction() // 通过prototype创建的方法可以在实例中访问 book1.proFunction() // 直接在构造函数中.的方法实例是无法访问的 // book1.setTime() // 只能通过构造函数来访问 Book.setTime() // privateFunction是无法访问的 // Book.privateFunction() // 继承 var SuperClass = function() { var id = 1 this.name = ['javascript'] this.superValue = function() { console.log('superValue is true') console.log(id) } } SuperClass.prototype.getSuperValue = function() { return this.superValue() } var SubClass = function() { this.subValue = function() { console.log('this is subValue ') } } //继承父类 SubClass.prototype = new SuperClass() SubClass.prototype.getSubValue = function() { return this.subValue() } var sub = new SubClass() var sub2 = new SubClass() console.log(sub) // 多态 function Add() { function zero() { return 0 } function one(id) { return 0 + id } function two(id, name) { return 0 + id + name } this.print = function() { var arg = arguments var len = arg.length switch(len) { case 0: { return zero() } case 1: { return one(arg[0]) } case 2: { return two(arg[0], arg[1]) } } } } var add = new Add() console.log(add.print()) console.log(add.print(1)) console.log(add.print(1, 2)) } </script> </body> </html>
ES6
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>js的封装、继承与多态</title> </head> <body> <script> window.onload = function() { // 封装 class School { // 构造器:创建对象完成初始化操作 constructor(id, name) { this.id = id this.name = name } // 实例方法 schoolName() { console.log(this.name) } // 类方法 static schoolOnly() { console.log('我是类方法只可通过函数本身调用') } } // 继承 class Student extends School { constructor(sId, sName, id, name) { super(id, name) this.sId = sId this.sName = sName } studentName() { console.log(this.sName) } say() { console.log('I am a student') } } // 多态 class Teacher extends School { constructor(tId, tName, id, name) { super(id, name) this.tId = tId this.tName = tName } TeacherName() { console.log(this.tName) } say() { console.log('I am a teacher') } } // 测试 let school = new School(1, '第一小学') let student = new Student(10, 'Daming', 1, '第一小学') let teacher = new Teacher(100, 'MrLi', 1, '第一小学') console.log(student) console.log(teacher) student.studentName() student.schoolName() student.say() teacher.say() School.schoolOnly() } </script> </body> </html>
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· PPT革命!DeepSeek+Kimi=N小时工作5分钟完成?
· What?废柴, 还在本地部署DeepSeek吗?Are you kidding?
· 赶AI大潮:在VSCode中使用DeepSeek及近百种模型的极简方法
· DeepSeek企业级部署实战指南:从服务器选型到Dify私有化落地