Last active
October 18, 2017 03:05
-
-
Save nikahmadz/ebd65c24fa8e0bec5cb0ecaf8927fe75 to your computer and use it in GitHub Desktop.
Object Property Utility : Set readonly property value for an object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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