let code = (function($){
function var_export (mixedExpression, boolReturn) {
var echo = console.log;
var retstr = ''
var iret = ''
var value
var cnt = 0
var x = []
var i = 0
var funcParts = []
// We use the last argument (not part of PHP) to pass in
// our indentation level
var idtLevel = arguments[2] || 2
var innerIndent = ''
var outerIndent = ''
var getFuncName = function (fn) {
var name = (/\W*function\s+([\w$]+)\s*\(/).exec(fn)
if (!name) {
return '(Anonymous)'
}
return name[1]
}
var _makeIndent = function (idtLevel) {
return (new Array(idtLevel + 1))
.join(' ')
}
var __getType = function (inp) {
var i = 0
var match
var types
var cons
var type = typeof inp
if (type === 'object' && (inp && inp.constructor) &&
getFuncName(inp.constructor) === 'LOCUTUS_Resource') {
return 'resource'
}
if (type === 'function') {
return 'function'
}
if (type === 'object' && !inp) {
// Should this be just null?
return 'null'
}
if (type === 'object') {
if (!inp.constructor) {
return 'object'
}
cons = inp.constructor.toString()
match = cons.match(/(\w+)\(/)
if (match) {
cons = match[1].toLowerCase()
}
types = ['boolean', 'number', 'string', 'array']
for (i = 0; i < types.length; i++) {
if (cons === types[i]) {
type = types[i]
break
}
}
}
return type
}
var type = __getType(mixedExpression)
if (type === null) {
retstr = 'NULL'
} else if (type === 'array') {
outerIndent = _makeIndent(idtLevel - 2)
innerIndent = _makeIndent(idtLevel)
for (i in mixedExpression) {
value = var_export(mixedExpression[i], 1, idtLevel + 2)
value = typeof value === 'string' ? value.replace(/</g, '<')
.replace(/>/g, '>') : value
x[cnt++] = value
}
iret = x.join(',');
retstr = '[' + iret +']';
} else if (type === 'object') {
outerIndent = _makeIndent(idtLevel - 2)
innerIndent = _makeIndent(idtLevel)
for (i in mixedExpression) {
value = var_export(mixedExpression[i], 1, idtLevel + 2)
value = typeof value === 'string' ? value.replace(/</g, '<')
.replace(/>/g, '>') : value
x[cnt++] = i + ':' + value;
}
iret = x.join(',')
retstr = '{' + iret + '}'
} else if (type === 'function') {
funcParts = mixedExpression.toString().match(/function .*?\((.*?)\) \{([\s\S]*)\}/)
retstr = "create_function ('" + funcParts[1] + "', '" +
funcParts[2].replace(new RegExp("'", 'g'), "\\'") + "')"
} else if (type === 'resource') {
// Resources treated as null for var_export
retstr = 'NULL'
} else {
retstr = typeof mixedExpression !== 'string' ? mixedExpression
: "'" + mixedExpression.replace(/(["'])/g, '\\$1').replace(/\0/g, '\\0') + "'"
}
if (!boolReturn) {
echo(retstr)
return null
}
return retstr
}
var settings = {
name: 'Hello John',
code: 'CODE12345',
location: 'US'
};
return{
getSettings : function(name){
console.log('Access closure variables: ',eval(name));
},
setSettings: function(name, value){
console.log('Set closure variables.', eval(name+'='+(var_export(value, true))));
}
}
})();
//1. Check settings before overriding
code.getSettings('settings');
//2. To set settings (override existing closure variable):
code.setSettings('settings',{
name: 'CoreX',
code: 'HACK9876',
location: 'Unknown'
}
);
//3. To access new seetings:
code.getSettings('settings');