如何在 Javascript 赋值期间附加字符串值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2423868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to append a string value during assignment in Javascript?
提问by streetparade
Hey i dont know if that is possible but i want to set a given variable in js by reference.
嘿,我不知道这是否可行,但我想通过引用在 js 中设置给定的变量。
What i want to do is, that each time i pass a string to the function addstringthat the value of the textfield is added like +=
我想要做的是,每次我将一个字符串传递给函数时addstring,文本字段的值被添加为+=
function addstring(string)
{
document.getElementById("string").value = string; // its a textfield
}
How can i do that?
我怎样才能做到这一点?
采纳答案by N 1.1
+=works fine.
+=工作正常。
var str = "stack";
str += "overflow";
console.log(str); //alert(str); Use firebug!!
stackoverflow
堆栈溢出
回答by Morten Anderson
Javascript does not support passing parameters by reference.
Javascript 不支持通过引用传递参数。
This link offers a good breakdown and some workarounds- Passing by Value or reference
此链接提供了很好的细分和一些解决方法-按值或参考传递
回答by rahul
回答by T.J. Crowder
Your code example will work just fine with +=; complete example below. This suggests the problem you're having lies elsewhere.
您的代码示例将与+=; 下面的完整示例。这表明您遇到的问题出在别处。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>Test Page</title>
<style type='text/css'>
body {
font-family: sans-serif;
}
</style>
<script type='text/javascript'>
function addstring(string)
{
document.getElementById('string').value += string;
}
</script>
</head>
<body><div>
<input type='text' id='string' value=''>
<br><input type='button' value='One' onClick="addstring('one');">
<input type='button' value='Two' onClick="addstring('two');">
<input type='button' value='Three' onClick="addstring('three');">
</div></body>
</html>
回答by Matt Brock
You can clone the object by first converting it to JSON (watch out for circular references) and then parse it back again. Like so:
您可以通过首先将对象转换为 JSON(注意循环引用)然后再次解析它来克隆对象。像这样:
function clone(obj) {
return JSON.parse(JSON.stringify(obj));
}
This uses the internal browser's JSON routines (safer & faster than using an external resource). If you simply must have backwards compatibility you can download the core JSON routines from JSON.org.
这使用内部浏览器的 JSON 例程(比使用外部资源更安全和更快)。如果您只是必须具有向后兼容性,您可以从 JSON.org 下载核心 JSON 例程。
回答by Pentium10
document.getElementById("string").value = document.getElementById("string").value + string;
document.getElementById("string").value = document.getElementById("string").value + string;
回答by Terje Rosenlund
Javascript does not support passing parameters by reference - Not true
Javascript 不支持通过引用传递参数 - 不正确
Actually it does. Prototyping makes it possible to create true references to all kinds of javascript objects, including strings.
事实上确实如此。原型设计使创建对所有类型的 javascript 对象(包括字符串)的真正引用成为可能。
By true reference I mean when:
通过真正的参考,我的意思是:
- Changes made to a variable or object passed by reference is reflected on the actual variable beeing passed
- The change is seen by all references to the same variable
- Everyone knowing the reference may change the referenced variable
- 对通过引用传递的变量或对象所做的更改反映在传递的实际变量上
- 对同一变量的所有引用都可以看到更改
- 每个知道引用的人都可能会改变被引用的变量
To create a true reference to a variable:
要创建对变量的真正引用:
- Create a constructor function
- Prototype your reference variables on the constructor
- DO NOT declare variables with the same name inside the constructor function!
- Create a change() function which changes the prototyped variableor do so directly
- Optional: Create a change() function-reference inside your constructor which is set to the ChangeRef() function uppon creation
- 创建构造函数
- 在构造函数上原型化您的引用变量
- 不要在构造函数中声明同名变量!
- 创建一个更改原型变量的 change() 函数 或直接执行此操作
- 可选:在您的构造函数中创建一个 change() 函数引用,该引用在创建时设置为 ChangeRef() 函数
Changes made this way will be seen and may be changed by all other TestRef() objects
以这种方式进行的更改将被所有其他 TestRef() 对象看到并可能更改
function TestRef(s) {
this.string = 'Own test-string: ' + s;
this.change = ChangeRef;
}
function ChangeRef(s) {
TestRef.prototype.refStr = s;
return TestRef.prototype.refStr;
}
r = 'RefStr';
TestRef.prototype.refStr = r; // PROTOTYPE => 'RefStr', copy of r
s = new TestRef('s'); // Obj.string = Own test-string: s, Obj.refStr = RefStr
o = new TestRef('o'); // Obj.string = Own test-string: o, Obj.refStr = RefStr
ChangeRef('ChangedStr'); // Change referenced string!
TestRef.prototype.refStr; // => ChangedStr, referenced string changed
r; // => RefStr, original string intact
x = new TestRef('x'); // Obj.string = Own test-string: x, Obj.refStr = ChangedStr. New sees changed string
s; // => Obj.string = Own test-string: s, Obj.refStr = ChangedStr. Old sees changed string
o; // => Obj.string = Own test-string: o, Obj.refStr = ChangedStr. Old sees changed string
s.change('Changed by local function');
x; // => Obj.string = Own test-string: o, Obj.refStr = Changed by local function

