不同子类执行父类的同名方法会有不同的结果
function Base() {
Base.prototype.initfun = function() {
this.init()
}
}
function Sub1 () {
this.init = function() {
console.log('1')
}
}
function Sub2() {
this.init = function() {
console.log('2')
}
}
Sub1.prototype = new Base()
Sub2.prototype = new Base()
new Sub1().initfun ()
new Sub2().initfun ()
参数个数不同, 执行不同的代码
function fun() {
if(arguments.length === 0) {
console.log('参数为0')
} else if (arguments.length === 1) {
console.log('一个参数', arguments[0])
} else if (arguments.length === 2) {
console.log('两个参数', arguments[0], arguments[1])
}
}
fun()
fun(1)
fun(1, 2)
参数类型不同的多态
function fun2() {
if (typeof arguments[0] === 'object' && typeof arguments[1] === 'string') {
console.log('类型1')
}else {
console.log('类型2')
}
}
fun2({a:1}, 's'); fun2({a: 1}, 1)