Forum Stats

  • 3,769,514 Users
  • 2,252,971 Discussions
  • 7,875,069 Comments

Discussions

Marking a Property Non-Writable in an Object Literal in Nashorn

Kishori Sharan
Kishori Sharan Member Posts: 26
edited Sep 6, 2014 4:05PM in Java 8 Questions

Hello,

I am trying to use a non-writable property in an object literal in Nashorn in Java8 as follows:

'use strict';

var p =  { x: {value: 100, writable:false}};

p.x = 200; // should not allow to change x, but it does

print(p.x);

The code prints 200 where I am expecting an error because I have defined x as non-writable.

If I use the Object.defineProperty() function to create the property or set the property as non-writable, it works. The following code results in an error, as expected:

'use strict';

var p =  { x: {value: 100, writable:false}};

Object.defineProperty(p, "x", {writable:false});

p.x = 200; // An error

print(p.x);

My question is

Why is setting the writable attribute to false in the expression { x: {value: 100, writable:false}} does not work in the first case? Is it a Nashorn bug or I am missing something?

Thanks

Kishori