Javascript property assignment question

K

karl.lopes

Hi,
I am trying to understand assignment in javascript. Assume ECMAScript 5.
My Question is why does step 4 in the note below work. I would expect it to fail.

1. //Create an Object...
var buffer = Object.create(Object.prototype);

2. //Add a property
Object.defineProperty(buffer, "__data__", {value:[],writable:false,enumerable:false,configurable:false});

Note in setp 2 the property is completely closed to any modification.
In particular...

3.var buffer_copy = Object.create(buffer);

//I would expect the line below to fail.. Since in Step 2 __data__ is made //writable false. BUT It works and the assignment does take place.
//As per JavaScript the Definitive Guide pg 122: If o inherits a read-only //property named x, for example, then the assignment is not allowed.
4.buffer_copy.__data__ = [];
 
R

Richard Cornford

Hi,
I am trying to understand assignment in javascript.
Assume ECMAScript 5.
My Question is why does step 4 in the note below work.

By "work" in this case you appear to mean why does it not throw an
exception, because you have not demonstrated that it does "work" in
the sense of assigning a new array object to the - __data__ - property
of the object referred to by - buffer_copy -.
I would expect it to fail.

That is good because it did.
1. //Create an Object...
var buffer = Object.create(Object.prototype);

2. //Add a property
Object.defineProperty(buffer, "__data__",
{value:[],writable:false,enumerable:false,configurable:false});

Note in setp 2 the property is completely closed to any modification.
In particular...

3.var buffer_copy = Object.create(buffer);

//I would expect the line below to fail.. Since in Step 2 __data__
is made //writable false. BUT It works and the assignment does take
place.

You have not shown that to be the case, only that the line of code is
executed without an exception being thrown.
//As per JavaScript the Definitive Guide pg 122: If o inherits a
read-only //property named x, for example, then the assignment is
not allowed.

And "not allowed" (if perhaps not the best way of expression it) does
not necessarily mean an exception will be thrown.
4.buffer_copy.__data__ = [];

If you try:-

<!DOCTYPE HTML>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
var buffer = Object.create(Object.prototype);
Object.defineProperty(
buffer,
"__data__",
{
value:['xx'],//<- Note the value of element
0.
writable:false,
enumerable:false,
configurable:false
}
);
var buffer_copy = Object.create(buffer);

buffer_copy.__data__ = ['yy']; //<- Note the value of element 0.

alert(buffer_copy.__data__[0]); //alerts 'xx' not 'yy'

</script>
</body>
</html>

- the alert shows 'xx', which is the zero indexed element of the array
originally assigned to the property of - buffer -, and so the
assignment of a different array to the - __data__ - property of -
buffer_copy - has in fact failed, it just did so silently. Silent
failure has always been a possibility in javascript, for example when
attempting to delete a non-deletable variable no exceptions would be
thrown in ES3.

The possibly exists that an exception would have been thrown when the
assignment was attempted, but that only happens in 'strict mode' code
in ES5, and this code is not 'strict mode'.

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top