Shoutbox

Replace Problem - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: Replace Problem (/showthread.php?tid=98078)

Replace Problem by Spunky on 08-02-2011 at 08:21 PM

I'm having trouble with the following code:

Javascript code:
// x[0] is a string
// y is an array (that in these circumstances does NOT contain x[0]
if(strip(x[0]) in y){ //strip just removes non-alphanumeric characters
    var foo = bar.replace(x[0], y[x[0]]);
    Debug.Trace(x[0] + " - " + y[x[0]]);
}


If x[0] is set to "push", "join", "reverse", "pop" or any other array method then I get "Object Expected" error and it traces:

quote:
pop -
function pop() {
    [native code]
}



Any ideas why? Everything should just be treated as a string :s I'm probably being stupid
RE: Replace Problem by matty on 08-02-2011 at 08:33 PM

Couldn't you use .toString()?


RE: Replace Problem by Spunky on 08-02-2011 at 08:36 PM

Doesn't work. I get the same results


RE: Replace Problem by Eljay on 08-02-2011 at 08:48 PM

How was y defined? for..in loops The in keyword is for Objects, not Arrays. :P


RE: Replace Problem by whiz on 08-02-2011 at 08:48 PM

I'm assuming it's because, for objects, foo.bar is the same as  foo["bar"].  Seems calling []["foo"], where foo is an array method, always seems to do this.

Could you perhaps add a prefix or something to x as a workaround?


RE: Replace Problem by Spunky on 08-02-2011 at 08:52 PM

quote:
Originally posted by whiz
I'm assuming it's because, for objects, foo.bar is the same as  foo["bar"].  Seems calling []["foo"], where foo is an array method, always seems to do this.

Could you perhaps add a prefix or something to x as a workaround?

I'll try shortly. If that does fix it, it's a bit sloppy, but I want it working. For the purposes of the script, it doesn't really cause any errors, Just don't want all that in the debug =/

EDIT: Nope can't seem to do that without breaking the script. I need the value related to x[0] from y


quote:
Originally posted by Eljay
How was y defined? for..in loops The in keyword is for Objects, not Arrays. :P

Arrays are Objects :p
RE: Replace Problem by Eljay on 08-02-2011 at 09:00 PM

quote:
Originally posted by Spunky
quote:
Originally posted by Eljay
How was y defined? for..in loops The in keyword is for Objects, not Arrays. :P

Arrays are Objects :p

Well yeah, but the in keyword returns true because of all the built-in Array functions. Try using:

Javascript code:
if(y.hasOwnProperty(strip(x[0]))


???
RE: Replace Problem by Spunky on 08-02-2011 at 09:02 PM

Works perfect (Y) No dodgy try... catch statements for me :D


RE: Replace Problem by Matti on 08-03-2011 at 08:50 AM

How is y defined? If you're storing values in an Array variable using string keys, then you're storing them as properties in the Object and not as elements in the Array.

Javascript code:
var a = [];
a[0] = "hello"; // stored as array element
Debug.Trace(a.length); // = 1, as you would expect
a["foo"] = "bar"; // stored as object property
Debug.Trace(a.length); // = 1, problem?
 
Debug.Trace(a.shift()); // = "hello", as you would expect
Debug.Trace(a.shift()); // = undefined, instead of the 'expected' "bar"

If you don't need any of the Array features such as .length, .pop() or .slice(), simply make it an Object:
Javascript code:
var y = {};

That way, you don't need any of those .hasOwnProperty() checks to skip any unwanted methods from the prototype, since Object.prototype should be empty.

It'd also help if you gave your variables decent names. How will you know what x[0] and y were when you look at your code again in a few months?