2010-01-19 68 views
62

爲什麼這項工作(返回 「一,二,三」):爲什麼.join()不能使用函數參數?

var words = ['one', 'two', 'three']; 
$("#main").append('<p>' + words.join(", ") + '</p>'); 

這項工作(返回 「名單:111」):

var displayIt = function() { 
    return 'the list: ' + arguments[0]; 
} 
$("#main").append('<p>' + displayIt('111', '222', '333') + '</p>'); 

,但不是這個(返回空白):

var displayIt = function() { 
    return 'the list: ' + arguments.join(","); 
} 
$("#main").append('<p>' + displayIt('111', '222', '333') + '</p>'); 

我有什麼做的,我的「論據」的變量設置爲使用就可以了。加入()?

+0

參見:http://stackoverflow.com/questions/1424710/why-is-my-join-on-a-javascript- array-failing – Shog9 2010-01-19 04:56:24

+0

我已經修改了我的答案,以考慮您更新的問題 - 具體來說,「我必須做些什麼來完成此項工作?」部分。 – 2010-01-19 04:58:34

回答

80

它不起作用,因爲arguments對象不是數組,雖然它看起來像它。它沒有join方法:

>>> var d = function() { return '[' + arguments.join(",") + ']'; } 
>>> d("a", "b", "c") 
TypeError: arguments.join is not a function 

arguments轉換成數組,你可以這樣做:

var args = Array.prototype.slice.call(arguments); 

現在join將工作:

>>> var d = function() { 
    var args = Array.prototype.slice.call(arguments); 
    return '[' + args.join(",") + ']'; 
} 
>>> d("a", "b", "c"); 
"[a,b,c]" 

或者,你可以使用jQuery的makeArray,它將嘗試打開「幾乎排列」,如arguments到數組:

var args = $.makeArray(arguments); 

下面介紹一下Mozilla reference(我最喜歡這樣的事情的資源)有什麼看法吧:

The arguments object is not an array. It is similar to an array, but does not have any array properties except length . For example, it does not have the pop method. ...

The arguments object is available only within a function body. Attempting to access the arguments object outside a function declaration results in an error.

+3

或'Array.join(arguments,「,」)'(比如'Array.forEach(arguments,func);') – Anonymous 2010-01-19 04:59:37

+0

當你的一個參數是一個對象時會發生什麼? – user1876508 2013-08-04 23:16:44

+3

@匿名 TypeError:對象函數Array(){[native code]}沒有方法'join' – drAlberT 2013-12-19 12:00:07

0

arguments不是一個jQuery對象,只是一個普通的JavaScript對象。在嘗試撥打.join()之前將其擴展。我想,你可以這樣寫:

return 'the list:' + $(arguments)[0]; 

(我不是太熟悉的jQuery,只有樣機,所以我希望這不完全是假的。)

編輯:這是錯誤的!但是在他的迴應中,Doug Neiner描述了我想要完成的事情。

+2

這不太對; 'arguments'不是一個數組。相反,它是一個「類似數組的對象」。 – 2010-01-19 04:51:51

+0

正常的JS數組支持'join'。 – 2010-01-19 04:52:00

+0

如果參數不是數組,參數[0]和參數[1]怎麼給我傳遞給函數的正確值? – 2010-01-19 04:53:54

2

只需使用jQuery的效用函數makeArray

arguments不是一個數組,它是一個對象。但是,因爲它使「陣列狀」,你可以調用jQuery的效用函數makeArray,使其工作:

var displayIt = function() { 
    return 'the list: ' + $.makeArray(arguments).join(","); 
} 
$("#main").append('<p>' + displayIt('111', '222', '333') + '</p>'); 

這將輸出:

<p>the list: 111,222,333</p> 
1

您可以使用typeof運算來看看發生了什麼在這裏:

>>> typeof(['one', 'two', 'three']) 
"object" 
>>> typeof(['one', 'two', 'three'].join) 
"function" 
>>> typeof(arguments) 
"object" 
>>> typeof(arguments.join) 
"undefined" 

在這裏你可以看到typeof返回「對象」在這兩種情況下,但只有一個對象有一個連接函數定義。

19

如果你不感興趣的其他Array.prototype方法,並且要簡單地使用join,你可以直接調用它,而無需將其轉換爲一個數組:

var displayIt = function() { 
    return 'the list: ' + Array.prototype.join.call(arguments, ','); 
}; 

而且您可能會發現要知道有用逗號是默認分隔符,如果您未定義分隔符,則使用spec將使用逗號。

+0

+1這是最快和最安全的答案*如果你想要的只是調用連接而不是創建一個新的數組。調用僅用於連接的切片也會在連接處理類似數組的對象時分配內存。 – Ajax 2014-02-23 09:46:20

0

我不知道是否有將參數轉換爲數組一個簡單的方法,但你可以試試這個:

var toreturn = "the list:"; 
for(i = 0; i < arguments.length; i++) 
{ 
    if(i != 0) { toreturn += ", "; } 
    toreturn += arguments[i]; 
} 
2

您可以使用此jQuery .joinObj Extension/Plugin我做了。

正如你在撥弄看到,你可以按如下方式使用它:

$.joinObj(args, ","); 

$.(args).joinObj(","); 

插件代碼:

(function(c){c.joinObj||(c.extend({joinObj:function(a,d){var b="";if("string"===typeof d)for(x in a)switch(typeof a[x]){case "function":break;case "object":var e=c.joinObj(a[x],d);e!=__proto__&&(b+=""!=b?d+e:e);break;default:"selector"!=x&&"context"!=x&&"length"!=x&&"jquery"!=x&&(b+=""!=b?d+a[x]:a[x])}return b}}),c.fn.extend({joinObj:function(a){return"object"===typeof this&&"string"===typeof a?c.joinObj(this,a):c(this)}}))})(jQuery); 
+2

...爲什麼是-1?這是一個可以按照要求完成的工作插件? – SpYk3HH 2012-08-11 04:09:25

+3

我也討厭它,當人們-1我的文章沒有解釋.. +1爲你.. T^T – Kokizzu 2012-12-17 05:27:52

0

在那時你不能加入數組參數,因爲它們不是陣列,shown here

,所以你必須要麼第一把它們變成一個這樣的數組,

function f() { 
    var args = Array.prototype.slice.call(arguments, f.length); 
    return 'the list: ' + args.join(','); 
} 

或類似這樣的,有點短

function displayIt() { 
    return 'the list: ' + [].join.call(arguments, ','); 
} 

如果你正在使用類似babel或一個兼容的瀏覽器來使用es6的功能,你也可以使用rest參數來做到這一點。

function displayIt(...args) { 
    return 'the list: ' + args.join(','); 
} 

displayIt('111', '222', '333'); 

這將讓你做更酷的東西一樣

function displayIt(start, glue, ...args) { 
    return start + args.join(glue); 
} 

displayIt('the start: ', '111', '222', '333', ','); 
相關問題