「JavaScript/Set」の版間の差分

削除された内容 追加された内容
Ef3 (トーク | 投稿記録)
→‎Polyfill: cleanup.
タグ: 2017年版ソースエディター
Ef3 (トーク | 投稿記録)
→‎Polyfill: cleanup, more.
タグ: 2017年版ソースエディター
 
113 行
},
intersection: function (other) {
return this.filter(e => (other.has(e)))
},
difference: function (other) {
return this.filter(e => (!other.has(e)))
},
superset_p: function (other) {
130 行
},
}).forEach(pair => {
const [method, func] = pair
Object.defineProperty(Set.prototype, pair[0]method, {
value: pair[1]func
})
})
 
letconst a = new Set([0, 1, 2]),
b = new Set([2, 3, 4]),
c = new Set([0, 1, 2, 3, 4, 5, 6])
 
console.log("a = ", a, ", b = ", b)
const ary = [
console.log(a.filter(x => x % 2))
"a",
//console.log(a.filter(1))
"b",
console.log(a.map(x => x * 2))
"c",
console.log(a.union(b))
console.log( "a.map(x => x * 2))",
console.log(a.intersection(b))
console.log( "a.union(b))",
console.log(a.difference(b))
console.log( "a.intersection(b))",
console.log(a.superset_p(c))
console.log( "a.difference(b))",
console.log(a.subset_p(c))
console.log( "a.superset_p(c))",
console.log( "a.subset_p(c))",
]
ary.forEach(x => console.log(x, "=>", eval(x)))
</syntaxhighlight>
;実行結果:<syntaxhighlight lang="text">
a = > Set(3) { 0, 1, 2 } ,
b = > Set(3) { 2, 3, 4 }
c => Set(17) { 0, 1, 2, 3, 4, 5, 6 }
a.map(x => x * 2) => Set(3) { 0, 2, 4 }
a.union(b) => Set(5) { 0, 1, 2, 3, 4 }
a.intersection(b) => Set(1) { 2 }
a.difference(b) => Set(2) { 0, 1 }
a.superset_p(c) => false
a.subset_p(c) => true
true
</syntaxhighlight>