• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright JS Foundation and other contributors, http://js.foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15var obj = {
16  prop: function() {},
17  foo: 'bar'
18};
19
20// New properties may be added, existing properties may be changed or removed
21obj.foo = 'baz';
22obj.lumpy = 'woof';
23delete obj.prop;
24
25var o = Object.freeze(obj);
26
27assert(Object.isFrozen(obj) === true);
28
29// Now any changes will fail
30obj.foo = 'quux'; // silently does nothing
31assert (obj.foo === 'baz');
32
33obj.quaxxor = 'the friendly duck'; // silently doesn't add the property
34assert (obj.quaxxor === undefined);
35
36// ...and in strict mode such attempts will throw TypeErrors
37function fail(){
38  'use strict';
39
40  try {
41    obj.foo = 'sparky'; // throws a TypeError
42    assert (false);
43  } catch (e) {
44    assert (e instanceof TypeError);
45  }
46
47  try {
48    delete obj.foo; // throws a TypeError
49    assert (false);
50  } catch (e) {
51    assert (e instanceof TypeError);
52  }
53
54  try {
55    obj.sparky = 'arf'; // throws a TypeError
56    assert (false);
57  } catch (e) {
58    assert (e instanceof TypeError);
59  }
60}
61
62fail();
63
64// Attempted changes through Object.defineProperty will also throw
65
66try {
67  Object.defineProperty(obj, 'ohai', { value: 17 }); // throws a TypeError
68  assert (false);
69} catch (e) {
70  assert (e instanceof TypeError);
71}
72
73try {
74  Object.defineProperty(obj, 'foo', { value: 'eit' }); // throws a TypeError
75  assert (false);
76} catch (e) {
77  assert (e instanceof TypeError);
78}
79