summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--chrome/browser/resources/shared/js/cr.js10
-rw-r--r--chrome/browser/resources/shared/js/cr_test.html27
2 files changed, 35 insertions, 2 deletions
diff --git a/chrome/browser/resources/shared/js/cr.js b/chrome/browser/resources/shared/js/cr.js
index 3c3a37e..77bc40d 100644
--- a/chrome/browser/resources/shared/js/cr.js
+++ b/chrome/browser/resources/shared/js/cr.js
@@ -300,8 +300,14 @@ const cr = (function() {
function define(name, fun) {
var obj = exportPath(name);
var exports = fun();
- for (var key in exports) {
- obj[key] = exports[key];
+ for (var propertyName in exports) {
+ // Maybe we should check the prototype chain here? The current usage
+ // pattern is always using an object literal so we only care about own
+ // properties.
+ var propertyDescriptor = Object.getOwnPropertyDescriptor(exports,
+ propertyName);
+ if (propertyDescriptor)
+ Object.defineProperty(obj, propertyName, propertyDescriptor);
}
}
diff --git a/chrome/browser/resources/shared/js/cr_test.html b/chrome/browser/resources/shared/js/cr_test.html
index 43c3628..b20281c 100644
--- a/chrome/browser/resources/shared/js/cr_test.html
+++ b/chrome/browser/resources/shared/js/cr_test.html
@@ -244,6 +244,33 @@ function testAddSingletonGetter() {
x, z);
}
+function testWithDoc() {
+ var d = {};
+
+ assertEquals(document, cr.doc);
+
+ cr.withDoc(d, function() {
+ assertEquals(d, cr.doc);
+ });
+
+ assertEquals(document, cr.doc);
+}
+
+function testDefineWithGetter() {
+ var v = 0;
+ cr.define('foo', function() {
+ return {
+ get v() {
+ return v;
+ }
+ }
+ });
+
+ assertEquals(0, foo.v);
+
+ v = 1;
+ assertEquals(1, foo.v);
+}
</script>