Why doesn't .join() work with function arguments?

后端 未结 8 1464
遇见更好的自我
遇见更好的自我 2021-02-01 11:59

Why does this work (returns \"one, two, three\"):

var words = [\'one\', \'two\', \'three\'];
$(\"#main\").append(\'

\' + words.join(\", \") + \'

8条回答
  •  悲&欢浪女
    2021-02-01 12:24

    You could use this jQuery .joinObj Extension/Plugin I made.

    As you'll see in that fiddle, you can use it as follows:

    $.joinObj(args, ",");
    

    or

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

    Plugin Code:

    (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);
    

提交回复
热议问题