开发者

JS OOP Built In Object Array

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?


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.


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('').


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜