| 1 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 |
|
| 5 |
js> load('testsrc/doctests/util.js'); |
| 6 |
|
| 7 |
js> Object.keys; |
| 8 |
function keys() { [native code for Object.keys, arity=1] } |
| 9 |
|
| 10 |
js> expectTypeError(function() { Object.keys() }) |
| 11 |
js> [undefined, null, true, 1, 'hello'].forEach(function(value) { |
| 12 |
> expectTypeError(function() { Object.keys(value) }) |
| 13 |
> }) |
| 14 |
|
| 15 |
js> Object.keys({}).toSource(); |
| 16 |
[] |
| 17 |
js> Object.keys({a:2}).toSource(); |
| 18 |
["a"] |
| 19 |
js> Object.keys({a:1, b:2}).toSource(); |
| 20 |
["a", "b"] |
| 21 |
js> Object.keys({'a.b':1, 'c d':2}).toSource(); |
| 22 |
["a.b", "c d"] |
| 23 |
|
| 24 |
js> Object.keys([]).toSource(); |
| 25 |
[] |
| 26 |
js> Object.keys(['a', 'b', 'c']).toSource(); |
| 27 |
["0", "1", "2"] |
| 28 |
|
| 29 |
js> function UserDefined() { this.a = 1; this.b = 2 }; |
| 30 |
js> var obj = new UserDefined() |
| 31 |
js> Object.keys(obj).toSource() |
| 32 |
["a", "b"] |
| 33 |
|
| 34 |
js> UserDefined.prototype.c = 3; |
| 35 |
3 |
| 36 |
js> Object.keys(obj).toSource() |
| 37 |
["a", "b"] |
| 38 |
|
| 39 |
js> // test properties of result are enumerable |
| 40 |
js> for (var p in Object.keys({a:2, b:3})) print(p) |
| 41 |
0 |
| 42 |
1 |
| 43 |
|
| 44 |
js> // test that properties of result are writable |
| 45 |
js> var k = Object.keys({a:2, b:3}); |
| 46 |
js> k[1] = 'c'; k.toSource(); |
| 47 |
["a", "c"] |
| 48 |
|
| 49 |
js> // test that properties of result are configurable |
| 50 |
js> var k = Object.keys({a:2, b:3}) |
| 51 |
js> delete k[1]; |
| 52 |
true |
| 53 |
js> k |
| 54 |
a, |
| 55 |
js> // TODO test that the attributes of the properties can be changed |
| 56 |
|
| 57 |
js> var k = Object.keys({ a:1, 3:2 }); |
| 58 |
js> typeof k[1]; |
| 59 |
string |