canmove, Confirmed users
2,056
edits
(we can definitely do new-less construction; remove comment questioning that) |
(describe multi-value property pattern) |
||
Line 58: | Line 58: | ||
// Delete a property. | // Delete a property. | ||
delete panel.height; | delete panel.height; | ||
Properties that can accept either a single or multiple unordered values should be settable to either a single or an array of values, and it should be possible to add and remove individual values from their set of values by calling add and remove methods on the property: | |||
let Foo = require("foo").Foo; | |||
let foo = new Foo(); | |||
foo.bar = ["a", "b", "c"]; | |||
foo.bar.add("d"); | |||
foo.bar.remove(["a", "b"]); | |||
// foo.bar now has the values "c" and "d". | |||
It should also be possible to enumerate multi-value properties via for each.. in loops: | |||
for each (let val in foo.bar) { ... } | |||
The add/remove methods on multi-value properties should remove primitive values (strings, numbers, booleans) by value and non-primitive values by reference: | |||
foo.bar.add("a"); | |||
foo.bar.add(function() alert("foo!")); | |||
let c = function() alert("bar!"); | |||
foo.bar.add(c); | |||
foo.bar.remove("a"); | |||
// a is removed (the string "a" is a primitive value). | |||
foo.bar.remove(function() alert("foo!")); | |||
// function() alert("foo!") is not removed (the function being removed | |||
// refers to a different instance of a function that was added). | |||
foo.bar.add(c); | |||
// c is removed (it refers to the same instance of a function that was added). | |||
It should be possible to construct instances of an object by calling its constructor without the <code>new</code> operator. | It should be possible to construct instances of an object by calling its constructor without the <code>new</code> operator. | ||
Line 94: | Line 122: | ||
A quick technique for differentiating these is to consider the simple imperative sentence "[verb] it": "upload it" sounds right, while "sign in it" doesn't (it should be "sign it in"), suggesting that the former is a verb comprising a stem and affix while the latter is a phrasal verb. | A quick technique for differentiating these is to consider the simple imperative sentence "[verb] it": "upload it" sounds right, while "sign in it" doesn't (it should be "sign it in"), suggesting that the former is a verb comprising a stem and affix while the latter is a phrasal verb. | ||
Event callback properties should be multi-value properties, so it should be possible to specify multiple callbacks for an event by assigning an array to the event's callback property or by calling its <code>add</code> and <code>remove</code> methods: | |||
let foo = function() { ... }; | let foo = function() { ... }; |