function Animal() {
this.name = 'name'
}
// 实例化
new Animal()
class Animal {
constructor() {
this.name = 'name'
}
}
// 实例化
new Animal()
function Parent() {
this.name = 'Parent'
}
Parent.prototype.say = function(){
console.log('hello')
}
function Child() {
Parent.call(this)
this.type = 'Child'
}
console.log(new Parent())
console.log(new Child())
function Parent() {
this.name = 'Parent'
this.arr = [1, 2, 3]
}
Parent.prototype.say = function(){
console.log('hello')
}
function Child() {
this.type = 'Child'
}
Child.prototype = new Parent()
let s1 = new Child()
let s2 = new Child()
s1.arr.push(4)
console.log(s1.arr, s2.arr)
console.log(new Parent())
console.log(new Child())
console.log(new Child().say())
// 父类
function Parent() {
this.name = 'Parent'
this.arr = [1, 2, 3]
}
Parent.prototype.say = function(){
console.log('hello')
}
// 子类
function Child() {
Parent.call(this)
this.type = 'Child'
}
// 避免父级的构造函数执行两次,共用一个 constructor
// 但是无法区分实例属于哪个构造函数
// Child.prototype = Parent.prototype
// 改进:创建一个中间对象,再修改子类的 constructor
Child.prototype = Object.create(Parent.prototype)
Child.prototype.constructor = Child
// 实例化
let s1 = new Child()
let s2 = new Child()
let s3 = new Parent()
s1.arr.push(4)
console.log(s1.arr, s2.arr) // [1, 2, 3, 4] [1, 2, 3]
console.log(s2.constructor) // Child
console.log(s3.constructor) // Parent
console.log(new Parent())
console.log(new Child())
console.log(new Child().say())
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有