rhino1.7.7.testsrc.doctests.object.keys.doctest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rhino Show documentation
Show all versions of rhino Show documentation
Rhino is an open-source implementation of JavaScript written entirely in Java. It is typically
embedded into Java applications to provide scripting to end users.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
js> load('testsrc/doctests/util.js');
js> Object.keys;
function keys() { [native code for Object.keys, arity=1] }
js> expectTypeError(function() { Object.keys() })
js> [undefined, null, true, 1, 'hello'].forEach(function(value) {
> expectTypeError(function() { Object.keys(value) })
> })
js> Object.keys({}).toSource();
[]
js> Object.keys({a:2}).toSource();
["a"]
js> Object.keys({a:1, b:2}).toSource();
["a", "b"]
js> Object.keys({'a.b':1, 'c d':2}).toSource();
["a.b", "c d"]
js> Object.keys([]).toSource();
[]
js> Object.keys(['a', 'b', 'c']).toSource();
["0", "1", "2"]
js> function UserDefined() { this.a = 1; this.b = 2 };
js> var obj = new UserDefined()
js> Object.keys(obj).toSource()
["a", "b"]
js> UserDefined.prototype.c = 3;
3
js> Object.keys(obj).toSource()
["a", "b"]
js> // test properties of result are enumerable
js> for (var p in Object.keys({a:2, b:3})) print(p)
0
1
js> // test that properties of result are writable
js> var k = Object.keys({a:2, b:3});
js> k[1] = 'c'; k.toSource();
["a", "c"]
js> // test that properties of result are configurable
js> var k = Object.keys({a:2, b:3})
js> delete k[1];
true
js> k
a,
js> // TODO test that the attributes of the properties can be changed
js> var k = Object.keys({ a:1, 3:2 });
js> typeof k[1];
string
© 2015 - 2024 Weber Informatics LLC | Privacy Policy