原型的一道題
// const F = function () {}
function F(){}
// eslint-disable-next-line no-new-func
// const F = new Function()
// eslint-disable-next-line no-extend-native
Object.prototype.a = function () {
console.log('a')
}
// eslint-disable-next-line no-extend-native
Function.prototype.b = function () {
console.log('b')
}
const f = new F()
f.a()
F.b()
f.b()
// 结果:a,b,f.b()is not a function....
自以为完全理解js原型那一套的我,还是栽在了这道题上。
Function.prototype.b是给所有函数的原型加了个属性,f只是new出来的一个对象。所以
F.b()有结果而f.b()报错
浙公网安备 33010602011771号