递归连接javascript函数参数 [英] recursively concatenating a javascript functions arguments

查看:30
本文介绍了递归连接javascript函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个 javascript 难题,询问:编写一行 JavaScript 代码,将传递给函数的所有字符串连接起来:

<前><代码>function concatenate(/*任意数量的字符串*/){var string =/*你在这里的一行*/返回字符串;}

@meebo

看到函数参数被表示为一个索引对象可能是一个数组,我认为可以以递归方式完成.但是我的递归实现抛出了一个错误.--conc.arguments.shift 不是函数";--

<前><代码>函数浓度(){如果(conc.arguments.length === 0)返回 "";别的返回 conc.arguments.shift() + conc(conc.arguments);}

似乎 conc.arguments 不是数组,但可以通过数字索引访问并具有长度属性???令人困惑 - 请分享意见和其他递归实现.

谢谢

解决方案

arguments 据说是一个类似数组的对象.正如您已经看到的,您可以通过索引访问它的元素,但是您没有可以使用所有的 Array 方法.类似数组的对象的其他示例是由 getElementsByTagName() 或 getElementsByClassName() 返回的 HTML 集合.jQuery,如果你曾经使用过它,也是一个类似数组的对象.在查询了一些 DOM 对象后,在 DOM 选项卡中使用 Firebug 检查生成的 jQuery 对象,你就会明白我的意思.

这是我对 Meebo 问题的解决方案:

function conc(){if (arguments.length === 0)返回 "";别的返回 Array.prototype.slice.call(arguments).join(" ");}警报(浓度(a",b",c"));

Array.prototype.slice.call(arguments) 是一个很好的技巧,可以将我们的 arguments 转换为真正的 Array 对象.在 Firefox 中 Array.slice.call(arguments) 就足够了,但它在 IE6 中不起作用(至少),所以通常使用前一个版本.此外,这个技巧对 IE6 中的 DOM API 方法返回的集合不起作用(至少);它会抛出一个错误.顺便说一句,可以使用 apply 代替 call.

关于类数组对象的一点解释.在 JavaScript 中,您几乎可以使用任何东西来命名对象的成员,数字也不例外.所以你可以构造一个看起来像这样的对象,它是完全有效的 JavaScript:

var Foo = {酒吧:功能(){alert('我是酒吧');},0:函数(){alert('我是1');},长度:1}

上面的对象是一个类数组对象有两个原因:

  1. 它的成员名称是数字,所以它们就像数组索引
  2. 它有一个 length 属性,没有它你就不能把对象转换成一个真正的数组,构造如下:Array.prototype.slice.call(Foo);

Function 对象的 arguments 对象与 Foo 对象非常相似,只是它有其特殊用途.

I came across a javascript puzzle asking: Write a one-line piece of JavaScript code that concatenates all strings passed into a function:


    function concatenate(/*any number of strings*/) {
      var string = /*your one line here*/
      return string;
    } 

@ meebo

Seeing that the function arguments are represented as an indexed object MAYBE an array, i thought can be done in a recursive way. However my recursive implementation is throwing an error. --"conc.arguments.shift is not a function" --


    function conc(){
        if (conc.arguments.length === 0) 
            return "";
        else 
            return conc.arguments.shift() + conc(conc.arguments);
}

it seems as though conc.arguments is not an array, but can be accessed by a number index and has a length property??? confusing -- please share opinions and other recursive implementations.

Thanks

解决方案

arguments is said to be an Array-like object. As you already saw you may access its elements by index, but you don't have all the Array methods at your disposal. Other examples of Array-like objects are HTML collections returned by getElementsByTagName() or getElementsByClassName(). jQuery, if you've ever used it, is also an Array-like object. After querying some DOM objects, inspect the resulting jQuery object with Firebug in the DOM tab and you'll see what I mean.

Here's my solution for the Meebo problem:

function conc(){
    if (arguments.length === 0)
        return "";
    else
        return Array.prototype.slice.call(arguments).join(" ");
}

alert(conc("a", "b", "c"));

Array.prototype.slice.call(arguments) is a nice trick to transform our arguments into a veritable Array object. In Firefox Array.slice.call(arguments) would suffice, but it won't work in IE6 (at least), so the former version is what is usually used. Also, this trick doesn't work for collection returned by DOM API methods in IE6 (at least); it will throw an Error. By the way, instead of call one could use apply.

A little explanation about Array-like objects. In JavaScript you may use pretty much anything to name the members of an object, and numbers are not an exception. So you may construct an object that looks like this, which is perfectly valid JavaScript:

var Foo = {
    bar : function() {
        alert('I am bar');
    },

    0 : function() {
        alert('I am 1');
    },

    length : 1
}

The above object is an Array-like object for two reasons:

  1. It has members which names are numbers, so they're like Array indexes
  2. It has a length property, without which you cannot transform the object into a veritable Array with the construct: Array.prototype.slice.call(Foo);

The arguments object of a Function object is pretty much like the Foo object, only that it has its special purpose.

这篇关于递归连接javascript函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆