Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #8595 > unrolled thread

Javascript property assignment question

Started bykarl.lopes@gmail.com
First post2011-11-23 22:06 -0800
Last post2011-11-24 04:10 -0800
Articles 2 — 2 participants

Back to article view | Back to comp.lang.javascript


Contents

  Javascript property assignment question karl.lopes@gmail.com - 2011-11-23 22:06 -0800
    Re: Javascript property assignment question Richard Cornford <Richard@litotes.demon.co.uk> - 2011-11-24 04:10 -0800

#8595 — Javascript property assignment question

Fromkarl.lopes@gmail.com
Date2011-11-23 22:06 -0800
SubjectJavascript property assignment question
Message-ID<13680863.200.1322114780589.JavaMail.geo-discussion-forums@prfb10>
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__ = []; 

[toc] | [next] | [standalone]


#8597

FromRichard Cornford <Richard@litotes.demon.co.uk>
Date2011-11-24 04:10 -0800
Message-ID<4935e527-e194-471b-b500-145fec7502ca@n14g2000vbn.googlegroups.com>
In reply to#8595
On Nov 24, 6:06 am, karl.lo...@gmail.com wrote:
> 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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web