Object Freeze Prototype Behaviour
Test for quirks in the behaviour of Object.freeze() when applied to an object prototype
author(s):
Pete Feltham
HTML
<link rel="stylesheet" href="http://code.jquery.com/qunit/git/qunit.css">
<script src="http://code.jquery.com/qunit/git/qunit.js"></script>
<h1 id="qunit-header">Unit Tests</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture"></div>
JavaScript
"use strict";
if (typeof QUnit === 'undefined') {
var assert = require("assert");
global.test = function(name, callback) {
assert.doesNotThrow(callback);
};
global.equal = assert.equal;
global.ok = assert.ok;
}
test('Frozen prototype with string property only', function() {
var Clazz = function() {
this.prop1 = 'instance';
};
Clazz.prototype.prop1 = 'initial';
Object.freeze(Clazz.prototype);
var instance = new Clazz();
ok(Object.isExtensible(instance), "instance should be extensible");
ok(!Object.isExtensible(Clazz.prototype), "prototype should not be extensible");
equal(false, Object.getOwnPropertyDescriptor(Clazz.prototype, 'prop1').writable, "prototype property 'prop1' should not be writable");
equal(true, instance.hasOwnProperty('prop1'), "instance should have own property 'prop1'");
equal(true, Object.getOwnPropertyDescriptor(instance, 'prop1').writable, "instance property 'prop1' should be writable");
equal('instance', instance.prop1);
});
test('Frozen prototype with function property, set to string in instance', function() {
var Clazz = function() {
this.prop1 = 'instance';
};
Clazz.prototype.prop1 = function() { return 'initial'; };
Object.freeze(Clazz.prototype);
var instance = new Clazz();
equal('instance', instance.prop1);
});
test('Frozen prototype with two function properties, set both to strings in instance', function() {
var Clazz = function() {
this.prop1 = 'instance';
this.prop2 = 'instance';
};
Clazz.prototype.prop1 = function() { return 'initial'; };
Clazz.prototype.prop2 = function() { return 'initial'; };
Object.freeze(Clazz.prototype);
var instance = new Clazz();
equal('instance', instance.prop1);
equal('instance', instance.prop2);
});
test('Frozen prototype with two function properties, set to string and function in instance via Object.defineProperty()', function() {
var Clazz = function() {
Object.defineProperty(this, 'prop1', { value:...