<!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>
原文:https://www.cnblogs.com/microtiger/p/13266407.html