简体   繁体   English

javascript对象中的字符串串联

[英]String concatenation within a javascript object

I'm sure there is a simple answer to this but I couldn't find it. 我敢肯定有一个简单的答案,但我找不到。

I know how to concatenate a normal string in JavaScript but how would I do it within an object? 我知道如何在JavaScript中连接普通字符串,但是如何在对象中进行处理呢?

I require this as I'm reading through an XML file and creating the HTML for a list of links. 在阅读XML文件并为链接列表创建HTML时,我需要这样做。 Each time I go through the loop I want to create a new <li> containing the link. 每次循环时,我都想创建一个包含链接的新<li> How do I get the current value of the string and then append the new link on the end? 如何获取字符串的当前值,然后在末尾附加新链接? Once I've read through the XML I'll append the HTML to the page. 阅读完XML后,我会将HTML附加到页面上。

I've tried: 我试过了:

carParks.links = carParks.links + "<li><a href=\"#\">car park</a></li>";

with no success. 没有成功。

Any help is much appreciated. 任何帮助深表感谢。

String concatenation with an object property is just the same as anything else. 带有对象属性的字符串串联与其他所有字符串相同。 Theoretically the code you have there should work, as long as carParks.links is a writeable property . 从理论上讲,只要carParks.links 是可写的属性 ,您那里的代码就应该起作用。 When you perform string concatenation using the + or += operators, except when using them as arithmetic operators, the operands are converted to strings. 当使用++=运算符执行字符串连接时(将它们用作算术运算符时除外),操作数将转换为字符串。 For example: 例如:

var carParks = {};
carParks.links = carParks.links + "Test";
// -> "undefinedTest", because carParks.links was undefined

If you're getting an error message, check that carParks is defined and is a JavaScript object with writeable properties (eg not part of an external interface). 如果收到错误消息,请检查carParks是否已定义并且是具有可写属性的JavaScript对象(例如,不是外部接口的一部分)。 If you're getting no error, make sure carParks.links is not a number. 如果没有错误,请确保carParks.links不是数字。 If this doesn't help, please post a little more of the surrounding code and I'll take another shot at it. 如果这样做没有帮助,请再发布一些周围的代码,我将再进行介绍。

if you generate tonns of links maybe try /w array.join(); 如果您生成大量链接,则可以尝试/ w array.join();

var tmp = [];

for ([iterate over xml]) {
     tmp.push("<li><a href=\"#\">car park</a></li>");
}

carParks.links = tmp.join('');

faster than repeated string concat 比重复的字符串连接更快

Or what if you create a html documentFragment and the browser do the concat for you... When a DocumentFragment is appended, just the ducument fragment cildNodes appends to the concrete element. 或者,如果您创建html documentFragment并由浏览器为您做连接,该怎么办...当附加DocumentFragment时,仅将文档片段cildNodes附加到具体元素上。 And the layout calculated once, not like per element append, and you don't have to care about string concat execution times... 而且布局只计算一次,而不像每个元素追加一样,并且您不必担心字符串concat的执行时间...

var docFrag = document.createDocumentFragment();
for ([iterate over xml]) {
     var current = document.createElement('li');
     current.innerHTML = "<a href=\"#\">car park</a>";
     docFrag.appendChild(current);
}

theElementToAppendMyList.appendChild(docFrag);

This is basic string concatenation, however depending on your performance considerations there are different ways you might want to accomplish this; 这是基本的字符串连接,但是根据您的性能考虑,您可能会希望采用不同的方法来完成此操作。 essentially it boils down to either using the string concatenation operators that the language gives you, or array joining. 从本质上讲,可以归结为使用该语言为您提供的字符串连接运算符,或者使用数组连接。 The former is pretty straight forward but slow on older browsers while the latter is faster. 前者非常简单,但在较旧的浏览器上速度较慢,而后者则较快。

In your example, you're using string concatenation just fine. 在您的示例中,您正在使用字符串连接。 Another way would be to use the += operator: 另一种方法是使用+=运算符:

carParks.links = carParks.links + "<li><a href=\"#\">car park</a></li>";

The array joining approach looks like the following: 数组连接方法如下所示:

var buffer = [];
while(node != null) {
   buffer.push("<li><a href=\"#\">car park</a></li>");
}
carParks.links = buffer.join('');

The array joining approach produces less garbage but I have seen it run more slowly on newer browsers with large (10,000+ items) lists. 数组连接方法产生的垃圾更少,但是我已经看到它在具有大列表(超过10,000个项目)的新型浏览器上运行得更慢。

您应该创建一个UL对象,并为每个所需的LI使用appendChild。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM