Skip to content

Instantly share code, notes, and snippets.

@nikahmadz
Last active October 18, 2017 03:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikahmadz/ebd65c24fa8e0bec5cb0ecaf8927fe75 to your computer and use it in GitHub Desktop.
Save nikahmadz/ebd65c24fa8e0bec5cb0ecaf8927fe75 to your computer and use it in GitHub Desktop.
Object Property Utility : Set readonly property value for an object
/*
Set readonly property value for an object
*/
// Utils
var
op=function(o,p,v){
o=o||{};
var f=Object.defineProperty;
if(!io(v)){opv(v)}
if(f){
f(o,p,v);
}else{
o[p]=v.value;
}
return o
},
ops=function(o,p){
o=o||{};
var f=Object.defineProperties;
if(f){
f(o,p);
}else{
for(var x in p){o[x]=p[x].value}
}
return o
},
opv=function(v,w){
return{value:v,writable:w||!1}
},
opvs=function(o,w){
for(var x in o){o[x]=opv(o[x],w)}
return o
};
// Usage Example
var object1=op({},'property',opv('value'));
var object2=ops({},{
property:opv('value')
}));
var object3=ops({},opvs({
property:'value'
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment