简体   繁体   English

JavaScript的新手:我可以将List传递给此函数,并且仍然可以使用吗?

[英]New to JavaScript: Can I pass a List into this function and have it still work?

As the title says, I'm new to JavaScript and not completely sure on certain syntax's. 就像标题所说的那样,我是JavaScript的新手,不确定某些语法。

After looking for a while I found the following function: 寻找了一段时间后,我发现了以下功能:

function makeUL(array) {
    // Create the list element:
    var list = document.createElement('ul');

    for (var i = 0; i < array.length; i++) {
        // Create the list item:
        var item = document.createElement('li');

        // Set its contents:
        item.appendChild(document.createTextNode(array[i]));

        // Add it to the list:
        list.appendChild(item);
    }

    // Finally, return the constructed list:
    return list;
}

All I need to know is if I pass a List of Strings into the functions parameter, will it still work, or when it says array is that defining what kind of variable it is? 我所需要知道的是,如果我将字符串列表传递到functions参数中,它是否仍然有效,或者当它说array定义了它是什么类型的变量时? If it won't currently work with a List what needs to change? 如果当前无法使用列表,则需要更改什么?

when it says array is that defining what kind of variable it is? 当它说数组是定义它是什么类型的变量时?

No. It is defining the name of the variable that the argument will be assigned to in the scope of the function. 否。它定义了在函数范围内参数将被分配给的变量的名称。

JavaScript (at least ES5 which you are using here) doesn't have any type enforcement for function arguments, so you can pass whatever values you like. JavaScript(至少您在此处使用的ES5)没有对函数参数进行任何类型强制,因此您可以传递所需的任何值。

The function does expect the value you pass to be an object with a length property and a number of properties with names that are integers (ie it expects the value to be like an array (see also duck typing )). 该函数确实希望您传递的值是一个具有length属性和多个具有整数名称的属性的对象(即,它希望该值一个数组(另请参见duck类型 ))。

List is not a standard JavaScript data type. 列表不是标准的JavaScript数据类型。 If you have created a List object, then you can pass it so long as it is array-like. 如果创建了List对象,则只要它像数组一样就可以传递它。

array is just the name of the expected parameter. array只是预期参数的名称。 It can be of any type (Javascript doesn't have types) 它可以是任何类型(Javascript没有类型)

So: Yes, your list will still "work" (as long as it supports the functions that are called on it, of course) 因此:是的,您的列表仍将“起作用”(当然,只要它支持在其上调用的功能)

You can pass any Array or Array-like object containing string items, no matter how the parameter is named, and your function will work as expected. 无论参数如何命名,都可以传递包含字符串项的任何Array类似Array的对象 ,并且函数将按预期工作。

Array-like object is an object, that has a length property with value >=0 and properties 0..length-1 . 类数组对象是一个对象,它的length属性值为> = 0 ,属性为0..length-1 Array-like object offen does not implement Array methods. 类似数组的对象见于Ger.Offen 没有实现阵列的方法。 The Array can be distinguished from Array-like object using a instanceof Array or Array.isArray(a) , while the typeof a returns 'object' in both cases. 可以使用a instanceof ArrayArray.isArray(a)将Array与类似Array的对象区分开,而两种情况下typeof a返回'object'

It will still work. 它仍然可以工作。 Truly speaking in javascript there is an array no list as such. 实话实说,在javascript中没有这样的数组。 Hence even if you call the function with below arguments: 因此,即使您使用以下参数调用该函数:

makeUL(["Cpp","Java","Javascript"])

The output would be : 输出将是:

<ul>
  <li>Cpp</li>
  <li>Java</li>
  <li>Javascript</li>
</ul>

I'm assuming you mean Array. 我假设你的意思是数组。 but i think your function is just returning a DOM element, so you wont directly see that in browser unless you append it into DOM tree. 但是我认为您的函数只是返回一个DOM元素,因此除非将其附加到DOM树中,否则您不会在浏览器中直接看到它。

var listofstrings = ['item 1','item 2','item 3'];
document.getElementByName('body').appendChild(makeUL(listofstring));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将可移动事件侦听器添加到元素列表,但仍将参数传递给它调用的函数? - How can I add a removable event listener to a list of elements, but still pass parameters to the function it calls? 我可以将此函数传递给Javascript中的另一个函数吗? - Can I pass this function to another function in Javascript? 我可以加密javascript文件,并且仍在客户端上运行它吗? - Can I encrypt a javascript file and still have it run on the client? 我可以将“新”匿名函数传递给addEventListener吗? - Can I pass “new” anonymous function to addEventListener 如何将新数组传递给函数? - How can I pass a new array to a function? 如何将参数的字符串列表传递给函数并将其分配为单独的参数 - How can I pass a string list of parameters to a function and have them assigned as separate arguments javascript没有将值传递给函数,但函数仍然具有它 - javascript not passing value to function, but function still have it 如何不通过争论仍然使功能工作? - How to not pass arguements and still make the function work? 我可以通过javascript函数传递值吗? - Can I pass a value out of a javascript function? 如何将 JavaScript function 传递给 Silverlight? - How can I pass a JavaScript function to Silverlight?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM