0
var v = new Array('Bill','Erik','Pam');
v.toString();
v.constructor;

In Firebug 1.7.3, FF 5.0.1

v.constructor //Returns "undefined, not Array();

also:

 var s = new String('Couch Potato');


    s.indexOf('Couch');
    s.slice(1,5);
    s.split(" ");
    s.join(' ');  //FF returns "s.join" is not a function.

Why is this?

1
  • 1
    join is an array function in JavaScript. Btw, v.constructor shows function Array() {[native code]} in the web console. Commented Aug 8, 2011 at 16:29

3 Answers 3

2

In FF 5.0.1, I get this result:

var arr = [1,2,3];

typeof arr.constructor // function

But without the typeof, I get this:

var arr = [1,2,3];

arr.constructor // [ undefined ]

...which does show undefined, but it is in an Array. I'm guessing this is simply a display issue in FireBug.

Try doing this:

var arr = [1,2,3];

var arr_2 = arr.constructor( 4,5,6 );

...you'll see that arr_2 is the Array you'd expect.

With regard to your .join() not being a function, it is because s is still a String, not an Array. This is because .split() does not change the String into an Array, but rather returns an Array.

You could do this,

s = s.split(" ");
var joined = s.join(' ');

... and it will work.

Sign up to request clarification or add additional context in comments.

2 Comments

In the Web Console, it shows function Array() {[native code]}.
@Felix: Yeah, looks like a FireBug display issue.
1

For the constructor property, apparently firebug has a bug. try alerting that value (alert([].constructor)) and you'll see that it is not undefined.
The join method is an array method and it's normal for firebug to show an error when calling it on a string. In javascript, the String is not an array of char like in other languages (probably you got confused).
The only way you can call the join method in your case would be after you transformed your string into an array : "abcd".split('').join('').

1 Comment

Your explanation follows the publication example. My experiment led me astray.
1

I'm not sure as for the constructor (it works OK on Chrome), but the join method is not of strings, neither of Strings.

You can join an array into a string, e.g.

[1, 2, 3].join(" ") === "1 2 3";

But the string cannot be joined - what should it do?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.