SOURCE

/**
 * 鸭子类型模拟
 * 寻找可以发出 duckSingSong 叫声的合唱团成员
 * 
 * choir - 合唱团
 * duck - 鸭子
 * chicken - 鸡
 * duckSingSong - 鸭子叫实现方法
 * checkJoinChoir - 是否可以加入合唱团
 * */ 

let choir = []

const duck = {
    name: "duck",
    duckSingSong:() => {
        console.log('ga ga ga')
    }
}

const chicken = {
    name: "chicken",
    duckSingSong: () => {
        console.log('ga ga ga')
    }
}

const checkJoinChoir = (joiner) => {
    if(joiner && typeof joiner.duckSingSong === 'function'){
        choir.push(joiner)
        console.log('加入合唱团成功')
        console.log(`合唱团成员: ${choir.map(item=>item.name).join('、')}`)
    }
}

checkJoinChoir(duck)
checkJoinChoir(chicken)

console 命令行工具 X clear

                    
>
console