var set = new Object();在接下来的几个练习中,我们将从头开始构建一个全功能的Set。对于本练习,只要该值中尚不存在该值,就创建一个将值添加到set集合的函数。例如:
set.foo = true;
//看看我们的集合中是否存在foo:
console.log(set.foo)// true
this.add = function(element){如果成功添加该值,则该函数应返回
//一些代码来为集合添加值
}
true
否则返回false
。 Set
类应该有一个add
方法。
testString: 'assert((function(){var test = new Set(); return (typeof test.add === "function")}()), "Your Set
class should have an add
method.");'
- text: 您的add
方法不应添加重复值。
testString: 'assert((function(){var test = new Set(); test.add("a"); test.add("b"); test.add("a"); var vals = test.values(); return (vals[0] === "a" && vals[1] === "b" && vals.length === 2)}()), "Your add
method should not add duplicate values.");'
- text: 成功添加值后, add
方法应返回true
。
testString: 'assert((function(){var test = new Set(); var result = test.add("a"); return (result != undefined) && (result === true);}()), "Your add
method should return true
when a value has been successfully added.");'
- text: add
重复值时, add
方法应返回false
。
testString: 'assert((function(){var test = new Set(); test.add("a"); var result = test.add("a"); return (result != undefined) && (result === false);}()), "Your add
method should return false
when a duplicate value is added.");'
```