private!
protected!
const! JSON parse directives
myJSON = {
const! Apple:"Macintosh", // read-only!
protected! Macintosh:"Delicious", // read-only! nocopy!
private! Delicious:"Always", // noread! nocopy!
private! { DeliciousAlso:"Always", ALocalOnlyPassword:"SavedLocallyInAFileWhichWillNotBeLoadedAtRuntimeOrTransferedOverTheNetwork"... },
}
myJSON.Apple // Macintosh
myJSON.Macintosh // Delicious
myJSON.Delicious // Error Access of property for read or assign is restricted!
let myVarA = myJSON.Apple // Macintosh
let myVarB = myJSON.Macintosh // null
let myVarC = myJSON.Delicious // Error Access of property for read or assign is restricted!
myVarA = myJSON.Apple = 100 // Error Cannot assign const to a value!
myVarB = myJSON.Macintosh = 100 // null
myVarC = myJSON.Delicious = 100 // Error Access of property for read or assign is restricted!
console.log( myJSON.Apple ) // Macintosh
console.log( myJSON.Macintosh ) // Delicious
console.log( myJSON.Delicious ) // Always
If you can't read it, you can't print it.
just fyi in real-world-problems, if i want a json-object to be immutable, an effective poorman's solution is to store it in JSON.stringify
'd form:
let readOnlyConfig = JSON.stringify({
Apple: "Macintosh", // read-only!
Macintosh: "Delicious" // read-only! nocopy!
});
console.log( JSON.parse(readOnlyConfig).Apple ); // Macintosh
console.log( JSON.parse(readOnlyConfig).Macintosh ); // Delicious
// plus the data is easier to message-pass (which is always a plus in javascript)
postMessage(readOnlyConfig);
@kaizhu256 There is Object.freeze for that. It's more performant, reliable and robust solution.
3 Likes
What's the use case (beyond a possible #key: value
private property for objects)?