javascript - Weird behaviour with 'use strict' and read only properties -
javascript - Weird behaviour with 'use strict' and read only properties -
on mdn strict mode reference page says
any assignment silently fails in normal code (assignment non-writable property, assignment getter-only property, assignment new property on non-extensible object) throw in strict mode
so, using example, doing next throws typeerror
"use strict"; var obj1 = {}; object.defineproperty(obj1, "x", { value: 42, writable: false }); obj1.x = 9; // throws typeerror
however ran illustration seems 'use strict' beingness little overzealous rule. here setup
definelol.js
object.defineproperty(object.prototype, 'lol', { value: 'wat' })
setlol.js
'use strict'; console.log('here 0'); var sugar = { lol: '123' } console.log('here 1'); var verbose = {}; verbose.lol = '123'; console.log('here 2'); console.log('sugar.lol:', sugar.lol); console.log('verbose.lol:', verbose.lol); console.log('object.prototype.lol:', object.prototype.lol);
app.js
require('./definelol.js'); require('./setlol.js');
running node app.js
gives
here 0 here 1 /pathto/setlol.js:10 verbose.lol = '123'; ^ typeerror: cannot assign read property 'lol' of #<object> @ object.<anonymous> (/pathto/setlol.js:10:13) @ module._compile (module.js:456:26) @ object.module._extensions..js (module.js:474:10) @ module.load (module.js:356:32) @ function.module._load (module.js:312:12) @ module.require (module.js:364:17) @ require (module.js:380:17) @ object.<anonymous> (/pathto/app.js:2:1) @ module._compile (module.js:456:26) @ object.module._extensions..js (module.js:474:10)
there couple of interesting things interesting output. first not trying set lol
property on object.prototype
trying set lol
property of verbose
. prove changed definelol.js
be
object.defineproperty(object.prototype, 'lol', { writable: true, value: 'wat' })
now, running node app.js
gives
here 0 here 1 here 2 sugar.lol: 123 verbose.lol: 123 object.prototype.lol: wat
the sec thing interesting original programme failed on verbose.lol = '123'
happy creating sugar
, setting lol
123. don't understand because seems way created sugar
should syntactic sugar way created verbose
see section 11.13.1 of spec:
when assignment occurs within strict mode code, lefthandside must not evaluate unresolvable reference. if referenceerror exception thrown upon assignment. lefthandside may not reference info property attribute value {[[writable]]:false}, accessor property attribute value {[[set]]:undefined}, nor non-existent property of object [[extensible]] internal property has value false. in these cases typeerror exception thrown.
in sample code, left-hand side of =
look is, in fact, reference info property "writable" flag set false
.
now sympathetic notion shouldn't apply inherited properties, can see there may strong counter-argument. object literal allows property created "own" property of new "sugar" object seems odd.
edit — clarity, issue here assignment object property assigning "own" property of object. assignments don't impact properties inheritance chain. thus, question posed involves next apparent contradiction: if property object prototype "writable" flag set false
prevents assignment property name on existing objects, why assignment property succeeds in course of study of evaluation of object literal?
there nice rationale this, or bug. both v8 , whatever firefox runtime called (something-monkey guess) deed same way.
javascript node.js use-strict defineproperty
Comments
Post a Comment