By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,180 Members | 1,133 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,180 IT Pros & Developers. It's quick & easy.

Google Chrome "differences"

P: n/a
I've augmented string objects (not the prototype) and none of the
popular browsers ever complained. Until Chrome came along. Chrome won't
accept something like

String.prototype.foo = function() {
if (!this.bar) {
this.bar = [];
}
// this.bar stays always undefined
}

Trying a workaround I came across

String.prototype.bar = [];

String.prototype.foo = function() {
this.bar.push({
o: this,
p: "baz"
});
return this;
}

var x = "foo";
var y = "foo";

x.foo();
y.foo();

alert("".bar[0].o === "".bar[1].o);

yields false in "all" browsers. Chrome returns true.

Any comments on that?

Gregor

--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Sep 3 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Gregor Kofler <us****@gregorkofler.atwrites:
I've augmented string objects (not the prototype) and none of the
popular browsers ever complained. Until Chrome came along. Chrome
won't accept something like

String.prototype.foo = function() {
if (!this.bar) {
this.bar = [];
}
// this.bar stays always undefined
}
.... and ...
yields false in "all" browsers. Chrome returns true.

Any comments on that?
Well spotted.
It's not standard-compliant.

It's a lousy standard at that point - requireing the creation of a new
object every time you access a property of a basic string value
.... like the *length*! - but still, it is the spec.

/L
--
Lasse Reichstein Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Sep 4 '08 #2

P: n/a
Gregor Kofler <us****@gregorkofler.atwrites:
I've augmented string objects (not the prototype) and none of the
popular browsers ever complained. Until Chrome came along. Chrome
won't accept something like

String.prototype.foo = function() {
if (!this.bar) {
this.bar = [];
}
// this.bar stays always undefined
A little testing shows the core problem:
---
String.prototype.foo = function() {
return typeof this;
}
alert("x".foo()); // alerts "string"
---

I.e., some optimization that prevents creating new objects, that
probably works well for built-in functions, is also applied to
user-added methods on String.prototype.
Same problem exists for Number and Boolean.

/L
--
Lasse Reichstein Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Sep 4 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.