下面是一段JS代码
我的水平很低,想知道怎么修改能让脚本语言javascript显示,谢谢各位高手的赐教!!!
//**********************************************************
// Core Functions
//**********************************************************
//----------------------------------------------------------
// Clone an object/array
//----------------------------------------------------------
// Don't use Object.prototype to pollute object data array
function $clone(obj){ if(typeof obj != "object") return obj; var newObj; if(obj instanceof Array){
// Clone Array
newObj = [];
for(var i=0; i<obj.length; i++){
if(typeof obj[i] == "object"){
newObj[i] = $clone(obj[i]);
}else{
newObj[i] = obj[i];
}
}
}else{
// Clone Object
newObj = {};
for(i in obj){
if(typeof obj[i] == "object"){
newObj[i] = $clone(obj[i]);
}else{
newObj[i] = obj[i];
}
}
} return newObj;}
//----------------------------------------------------------
// Extend an object if property not exist yet
//----------------------------------------------------------
function $extend(objTarget, objAdd, forceOverride){ var obj = $clone(objTarget); // always new, no pollution if(typeof obj != "object") return obj; for(var item in objAdd){
if(obj[item] == undefined || forceOverride) obj[item] = objAdd[item];
} return obj;}
//----------------------------------------------------------
// Convert Object to JSON String
//----------------------------------------------------------
function $toJSON(obj, param){ // paramters
var defaultParam = {
'indent': 0,
'indentText': '',
'delimiter': '',
'includeFunction': false
}; param = param ? $extend(param, defaultParam) : defaultParam; //execute
var indentString = '';
var prevIndentString = ''; if(param['indentText'] != ''){
param['indent']++;
prevIndentString = new Array(param['indent']).join(param['indentText']);
indentString = new Array(param['indent']+1).join(param['indentText']);
} switch(typeof(obj)){ case "object": if(obj instanceof Array){ var out = [];
for(var i=0; i<obj.length; i++){
var t = $toJSON(obj[i], param);
if(t){
out.push(indentString + t);
}
} out = "[" +
param["delimiter"] +
out.join("," + param["delimiter"]) +
param["delimiter"] +
prevIndentString + "]"; }else if(obj instanceof Date){ return "new Date(" +
obj.getFullYear() + "," + obj.getMonth() + "," + obj.getDate() + "," +
obj.getHours() + "," + obj.getMinutes() + "," + obj.getSeconds() + "," + obj.getMilliseconds() +
")"; }else if(obj instanceof Object){ var out = [];
for(label in obj){
var l = $toJSON(label);
var t = $toJSON(obj[label], param);
if(t){
out.push(indentString + l + ": " + t);
}
} out = "{" +
param["delimiter"] +
out.join("," + param["delimiter"]) +
param["delimiter"] +
prevIndentString + "}"; } break; case "string": var str = obj;
str = str.replace(/\\"/g, '\\\\"');
str = str.replace(/\r/g, '\\r');
str = str.replace(/\t/g, '\\t');
str = str.replace(/\n/g, '\\n');
str = str.replace(/\f/g, '\\f');
str = str.replace(/\"/g, '\\"'); out = '"' + str + '"';
break; case "number": out = isFinite(obj) ? String(obj) : 'null';
break;

解决方案 »

  1.   

    下面是一段JS代码
    我的水平很低,想知道怎么修改能让脚本语言javascript显示,谢谢各位高手的赐教!!!
    //**********************************************************
    // Core Functions
    //**********************************************************
    //----------------------------------------------------------
    // Clone an object/array
    //----------------------------------------------------------
    // Don't use Object.prototype to pollute object data array
    function $clone(obj){ if(typeof obj != "object") return obj; var newObj; if(obj instanceof Array){
    // Clone Array
    newObj = [];
    for(var i=0; i<obj.length; i++){
    if(typeof obj[i] == "object"){
    newObj[i] = $clone(obj[i]);
    }else{
    newObj[i] = obj[i];
    }
    }
    }else{
    // Clone Object
    newObj = {};
    for(i in obj){
    if(typeof obj[i] == "object"){
    newObj[i] = $clone(obj[i]);
    }else{
    newObj[i] = obj[i];
    }
    }
    } return newObj;}
    //----------------------------------------------------------
    // Extend an object if property not exist yet
    //----------------------------------------------------------
    function $extend(objTarget, objAdd, forceOverride){ var obj = $clone(objTarget); // always new, no pollution if(typeof obj != "object") return obj; for(var item in objAdd){
    if(obj[item] == undefined || forceOverride) obj[item] = objAdd[item];
    } return obj;}
    //----------------------------------------------------------
    // Convert Object to JSON String
    //----------------------------------------------------------
    function $toJSON(obj, param){ // paramters
    var defaultParam = {
    'indent': 0,
    'indentText': '',
    'delimiter': '',
    'includeFunction': false
    }; param = param ? $extend(param, defaultParam) : defaultParam; //execute
    var indentString = '';
    var prevIndentString = ''; if(param['indentText'] != ''){
    param['indent']++;
    prevIndentString = new Array(param['indent']).join(param['indentText']);
    indentString = new Array(param['indent']+1).join(param['indentText']);
    } switch(typeof(obj)){ case "object": if(obj instanceof Array){ var out = [];
    for(var i=0; i<obj.length; i++){
    var t = $toJSON(obj[i], param);
    if(t){
    out.push(indentString + t);
    }
    } out = "[" +
    param["delimiter"] +
    out.join("," + param["delimiter"]) +
    param["delimiter"] +
    prevIndentString + "]"; }else if(obj instanceof Date){ return "new Date(" +
    obj.getFullYear() + "," + obj.getMonth() + "," + obj.getDate() + "," +
    obj.getHours() + "," + obj.getMinutes() + "," + obj.getSeconds() + "," + obj.getMilliseconds() +
    ")"; }else if(obj instanceof Object){ var out = [];
    for(label in obj){
    var l = $toJSON(label);
    var t = $toJSON(obj[label], param);
    if(t){
    out.push(indentString + l + ": " + t);
    }
    } out = "{" +
    param["delimiter"] +
    out.join("," + param["delimiter"]) +
    param["delimiter"] +
    prevIndentString + "}"; } break; case "string": var str = obj;
    str = str.replace(/\\"/g, '\\\\"');
    str = str.replace(/\r/g, '\\r');
    str = str.replace(/\t/g, '\\t');
    str = str.replace(/\n/g, '\\n');
    str = str.replace(/\f/g, '\\f');
    str = str.replace(/\"/g, '\\"'); out = '"' + str + '"';
    break; case "number": out = isFinite(obj) ? String(obj) : 'null';
    break;