哇你居然在代码里面下毒系列--3
发布于 6 个月前 作者 spitWind 1948 次浏览 来自 分享
// [].__proto__.unique = function () {
//     return Array.from(new Set(this))
// }
Object.prototype.isEmpty = function (params) {
    return Object.keys(this).length == 0
}
// 然后不懂用in遍历的人就傻了
var a = [1, 2,2]
for (const key in a) {
    console.log(key) // 0,1,2,"isEmpty"
}
var b = { a: 20 }
for (const key in b) {
    console.log(key)// "a","isEmpty"
}
console.log(b.isEmpty())
// 推荐改成
Object.defineProperty([].__proto__, "unique", {
    enumerable:false,
   value: function(params) {
    return Array.from(new Set(this))
}})
console.log(a.unique())
1 回复

我就真的遇到过改原型链的人

回到顶部