function Father () {
this.value = true
}
Father.prototype.getFatherValue = function() {
return this.value
}
function Son () {
this.sonValue = false
}
Son.prototype = new Father()
Son.prototype.getSonValue = function() {
return this.sonValue
}
var ex1 = new Son()
console.log(ex1.getFatherValue()) // true
console.log(ex1.getSonValue()) // true
function Father () {
this.type = ['html', 'css', 'js']
}
function Son () {}
Son.prototype = new Father()
var ex1 = new Son()
var ex2 = new Son()
console.log(ex2.type) // ['html', 'css', 'js']
ex1.type.push('ts')
console.log(ex2.type) // ['html', 'css', 'js', 'ts']
function Father (id) {
this.type = ['html', 'css', 'js']
this.id = id
}
Father.prototype.showType = function() {
return this.type
}
function Son (id) {
Father.call(this, id)
}
var ex1 = new Son(1)
var ex2 = new Son(2)
ex1.type.push('ts')
console.log(ex1.type) // ['html', 'css', 'js', 'ts']
console.log(ex1.id) // 1
console.log(ex2.type) // ['html', 'css', 'js' ]
console.log(ex2.id) // 2
console.log(ex1.showType) // undefined
function Father (name) {
this.type = ['html', 'css', 'js']
this.name = name
}
Father.prototype.getName = function() {
console.log(this.name)
}
function Son (name, time) {
Father.call(this, name)
this.time = time
}
Son.prototype = new Father()
Son.prototype.getTime = function() {
console.log(this.time)
}
var ex1 = new Son('father', 2020)
ex1.type.push('ts')
console.log(ex1.type) // ['html', 'css', 'js', 'ts']
ex1.getName() // father
ex1.getTime() // 2020
var ex2 = new Son('son', 2021)
console.log(ex2.type) // ['html', 'css', 'js']
ex2.getName() // son
ex2.getTime() // 2021
function inheritObject(o) {
function F() {}
F.prototype = o
return new F()
}
var book = {
name: 'js',
other: ['html', 'css']
}
var ex1 = inheritObject(book)
ex1.other.push('ts')
ex1.name = 'js1'
console.log(ex1.name) // js1
console.log(ex1.other) // ["html", "css", "ts"]
var ex2 = inheritObject(book)
ex2.name = 'js2'
console.log(ex2.name) // js2
console.log(ex2.other) // ["html", "css", "ts"]
function inheritObject(o) {
function F() {}
F.prototype = o
return new F()
}
function createBook(obj) {
var o = new inheritObject(obj)
o.getName = function() {
console.log(this.name, '---')
}
return o
}
var book = {
name: 'js',
other: ['html', 'css']
}
var ex1 = createBook(book)
ex1.other.push('ts')
ex1.name = 'js1'
ex1.getName() // js1
console.log(ex1.other) // ["html", "css", "ts"]
var ex2 = createBook(book)
ex2.name = 'js2'
ex2.getName() // js2
console.log(ex2.other) // ["html", "css", "ts"]
function inheritObject(o) {
function F() {}
F.prototype = o
return new F()
}
function inheritPrototype(subClass, superClass) {
var p = inheritObject(superClass.prototype)
p.constructor = subClass
subClass.prototype = p
}
function SuperClass(name) {
this.name = name
this.colors = ['red', 'green', 'yellow']
}
SuperClass.prototype.getName = function() {
console.log(this.name)
}
function SubClass(name, time) {
SuperClass.call(this, name)
this.time = time
}
inheritPrototype(SubClass, SuperClass)
SubClass.prototype.getTime = function() {
console.log(this.time)
}
var ex1 = new SubClass('html', 2021)
var ex2 = new SubClass('css', 2021)
ex1.colors.push('blue')
console.log(ex1.colors) // ['red', 'green', 'yellow', 'blue']
console.log(ex2.colors) // ['red', 'green', 'yellow']
ex2.getName() // css
ex2.getTime() // 2021
ps: 此文章做个人平常记录,若是有幸帮到朋友,但求一赞