fix the discrepancy between JS engines used by Firefox and Opera

G

George

Functions are objects and as such inherit from Object.prototype.
I've tested this in Safari 4, it (sort-of) works :)

//Load the JSON lib
if (!JSON) {
 document.body.appendChild(
 document.createElement(
 'script')).src="http://www.JSON.org/json2.js";

}

delete Object.prototype.toSource;

if (!Object.prototype.toSource && JSON && JSON.stringify) {
 Object.prototype.toSource= function (txt) {
  function replacer (key, value, n) {
   if (typeof value === "function") {
    n= !((value.name === "") || (value.name === "anonymous"));
    value= "***FuNcTiOn"+ (n?"":"(")+ value+ (n?"":")")+
"nOiTcNuF***";
   }
   return value;
  }
  txt= JSON.stringify(this, replacer)
  return txt.replace(/("\*\*\*FuNcTiOn)|(nOiTcNuF\*\*\*")/g,"");
 };

}

function zero () { return 0; }
var identity = new Function('x', 'return x;');
var obj= { a:1, f:function (_a) { a=_a; } };
var arr = [1, 2, function () { } ];

alert(zero.toSource());
// return function zero() { return 0; }

alert(identity.toSource());
// return (function anonymous(x) {return x;})

alert(obj.toSource());
// return ({a:1, f:(function (_a) {a = _a;})})

alert(arr.toSource());
// return [1, 2, (function () { } )]

HI Jorge,

When I try your code, I found a slight issue need to fix, as below.

var problematicFun1 = new Function('x', 'return x+"\n"');
var problematicFun2 = new Function('x', 'return x');

alert(problematicFun1.toSource()); // this cause an error
alert(problematicFun1.toSource()); // this works, but how can I get
rid of the trailing '\n's.

Thanks,
Yan
 
G

Garrett Smith

Jorge said:
if (o.hasOwnProperty(property)) { ...

Introducing the change you proposed into a system introduces
significant consequences.

Any for-in loop that forgoes the call to hasOwnProperty will have bugs,
hidden or otherwise. And so developers will have to be aware of the side
effects of "for in" loops and use hasOwnProperty to avoid it.

Introducing modification to Object.prototype imposes a burden on the any
program where that code is included.

It requires all for-in loops to use hasOwnProperty (which adds a small
bit of clutter). This means that all existing code will have to be
looked at carefully, tested, and most likely changed to use
|o.hasOwnProperty|. Finally, the system will need retesting.

Those consequences can be avoided by not modifying Object.prototype.
 
J

Jorge

Introducing the change you proposed into a system introduces
significant consequences.

Any for-in loop that forgoes the call to hasOwnProperty will have bugs,
hidden or otherwise. And so developers will have to be aware of the side
effects of "for in" loops and use hasOwnProperty to avoid it.

Introducing modification to Object.prototype imposes a burden on the any
program where that code is included.

It requires all for-in loops to use hasOwnProperty (which adds a small
bit of clutter). This means that all existing code will have to be
looked at carefully, tested, and most likely changed to use
|o.hasOwnProperty|. Finally, the system will need retesting.

Those consequences can be avoided by not modifying Object.prototype.

Yeah, sure, then, don't ever use prototypal inheritance at all, 'cause
it's a despicable model... :)
 
J

Jorge

On Oct 8, 9:01 pm, George <[email protected]> wrote:
Functions are objects and as such inherit from Object.prototype.
I've tested this in Safari 4, it (sort-of) works :)
//Load the JSON lib
if (!JSON) {
 document.body.appendChild(
 document.createElement(
 'script')).src="http://www.JSON.org/json2.js";

delete Object.prototype.toSource;
if (!Object.prototype.toSource && JSON && JSON.stringify) {
 Object.prototype.toSource= function (txt) {
  function replacer (key, value, n) {
   if (typeof value === "function") {
    n= !((value.name === "") || (value.name === "anonymous"));
    value= "***FuNcTiOn"+ (n?"":"(")+ value+ (n?"":")")+
"nOiTcNuF***";
   }
   return value;
  }
  txt= JSON.stringify(this, replacer)
  return txt.replace(/("\*\*\*FuNcTiOn)|(nOiTcNuF\*\*\*")/g,"");
 };

function zero () { return 0; }
var identity = new Function('x', 'return x;');
var obj= { a:1, f:function (_a) { a=_a; } };
var arr = [1, 2, function () { } ];
alert(zero.toSource());
// return function zero() { return 0; }
alert(identity.toSource());
// return (function anonymous(x) {return x;})
alert(obj.toSource());
// return ({a:1, f:(function (_a) {a = _a;})})
alert(arr.toSource());
// return [1, 2, (function () { } )]

HI Jorge,

When I try your code, I found a slight issue need to fix, as below.

var problematicFun1 = new Function('x', 'return x+"\n"');
var problematicFun2 = new Function('x', 'return x');

alert(problematicFun1.toSource()); // this cause an error
alert(problematicFun1.toSource()); // this works, but how can I get
rid of the trailing '\n's.

You ought to put some effort in it, too, ISTM :)
 
J

Jorge

Functions are objects and as such inherit from Object.prototype.
I've tested this in Safari 4, it (sort-of) works :)
//Load the JSON lib
if (!JSON) {
 document.body.appendChild(
 document.createElement(
 'script')).src="http://www.JSON.org/json2.js";
}
delete Object.prototype.toSource;
if (!Object.prototype.toSource && JSON && JSON.stringify) {
 Object.prototype.toSource= function (txt) {
  function replacer (key, value, n) {
   if (typeof value === "function") {
    n= !((value.name === "") || (value.name === "anonymous"));
    value= "***FuNcTiOn"+ (n?"":"(")+ value+ (n?"":")")+
"nOiTcNuF***";
   }
   return value;
  }
  txt= JSON.stringify(this, replacer)
  return txt.replace(/("\*\*\*FuNcTiOn)|(nOiTcNuF\*\*\*")/g,"");
 };
}
function zero () { return 0; }
var identity = new Function('x', 'return x;');
var obj= { a:1, f:function (_a) { a=_a; } };
var arr = [1, 2, function () { } ];
alert(zero.toSource());
// return function zero() { return 0; }
alert(identity.toSource());
// return (function anonymous(x) {return x;})
alert(obj.toSource());
// return ({a:1, f:(function (_a) {a = _a;})})
alert(arr.toSource());
// return [1, 2, (function () { } )]
HI Jorge,
When I try your code, I found a slight issue need to fix, as below.
var problematicFun1 = new Function('x', 'return x+"\n"');
var problematicFun2 = new Function('x', 'return x');
alert(problematicFun1.toSource()); // this cause an error
alert(problematicFun1.toSource()); // this works, but how can I get
rid of the trailing '\n's.

You ought to put some effort in it, too, ISTM :)

//Load the JSON lib
if (!JSON) {
document.body.appendChild(
document.createElement(
'script')).src="http://www.JSON.org/json2.js";
}

delete Object.prototype.toSource;

if (!Object.prototype.toSource && JSON && JSON.stringify) {
Object.prototype.toSource= function (n, txt, fTable) {
n= 0, fTable= {};

function replacer (key, value) {
if (typeof value === "function") {
key= "***FuNcTiOn"+ (n++)+ "nOiTcNuF***";
fTable["\""+ key+ "\""]= value;
value= key;
}
return value;
}

txt= JSON.stringify(this, replacer);

n= /"\*\*\*FuNcTiOn(.*?)nOiTcNuF\*\*\*"/g;
return txt.replace(n, function (p, n, txt) {
if (fTable[txt= p] && (fTable.hasOwnProperty(p))) {
n= !fTable[p].name || (fTable[p].name === "anonymous");
txt= (n?"(":"")+ fTable[p]+ (n?")":"");
}
return txt;
});
};
}

o= {
a: function () { "anonymous"; },
b: function b () { "b"; },
zero: function zero () { return 0; },
obj: { a: 1, f: function (_a) { a=_a; }},
identity: new Function('x', 'return x;'),
array: [1, 2, function () { } ]
};

alert(o.toSource());
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top