我想这只是我不了解JavaScript的工作原理.假设我有一个数组,调用它arr1,其中有6个整数, [1,2,3,4,5,6].如果我创建一个新数组:
var arr2 = arr1
Run Code Online (Sandbox Code Playgroud)
(为了保持不变的副本arr1),当我改变时arr1,所反映的变化arr2.
基本上,我在操纵arr1.出于测试目的,我想要一个未更改的副本,arr1这样当我完成后,我可以在我的网页上安装它们并查看它们之间的差异.但是,当我进行更改时arr1,这种变化反映在arr2.
任何人都可以解释为什么会发生这种情况并可能解决它 我更感兴趣的是为什么会发生这种情况而不是如何修复它.
一种方法是创建arr2一个单独的数组,并使用for循环用arr1数据填充它
for(int i = 0; i < arr1.length; i++) arr2[i] = arr1[i]
Run Code Online (Sandbox Code Playgroud)
但是,如果阵列很大,那可能会很昂贵.任何帮助表示赞赏.
我确定我在这里遗漏了一些明显的东西,但是我希望changeMe方法的参数可以"通过引用"传递 - 换句话说,对函数内部参数的更改将改变函数外部的变量.
以下内容在jsfiddle中运行,使用Chrome的F12开发人员工具显示控制台输出.http://jsfiddle.net/fzEpa/
var object1 = { Property1: 'Value1' };
changeMe(object1);
console.log(object1);
function changeMe(refToObject) {
console.log(refToObject);
refToObject = { Property1: 'Value2' };
console.log(refToObject);
}
Run Code Online (Sandbox Code Playgroud) 我有两个变量是字符串:month1Digit1和month1Digit2.它们一起组成一个月的十进制数字(01-12),因此month1Digit1总是0或1,而month1Digit2可以是0以外的任何数字.现在我有几个,如month2Digit1,等等.想要一个可以从这些变量中确定月份名称的函数.但我不想为每个组编写一个单独的函数,因为它有不同的变量.从搜索周围看起来我需要用参数做一个函数,但我不确定这是如何工作的.我尝试了以下方法:
var month1Digit1 = "1";
var month1Digit2 = "2";
function getMonthName (month) {
if (month == "1") { month = "January" }
else if (month == "2") { month = "February" }
else if (month == "3") { month = "March" }
else if (month == "4") { month = "April" }
else if (month == "5") { month = "May" }
else if (month == "6") { month = "June" }
else if (month == "7") { month = …Run Code Online (Sandbox Code Playgroud) 我有一个像js的对象
[{"name":"John","group":"t1..." .....}]
Run Code Online (Sandbox Code Playgroud)
所以要从同一个t1创建新用户,我循环使用obj
var new_user;
for(var i=0; i<users.length; i++) {
if(users[i].group === 'group name goes here') {
new_user = users[i];
break;
}
}
Run Code Online (Sandbox Code Playgroud)
然后只是推它使用users.push(new_user);然后我尝试改变使用的名称
var l = users.length - 1; users[l].name = 'Jack';
Run Code Online (Sandbox Code Playgroud)
它会更改所有用户的名称.
[{"name":"Jack","group":"t1..." .....},{"name":"Jack","group":"t1..." .....}]
Run Code Online (Sandbox Code Playgroud)
我知道我做错了什么.我感谢任何帮助.
我在我的应用程序中使用了svg图标并使用了svg来绘制图表.当我使用html2canvas生成图像时,所有svgs都被忽略并且生成的图像没有所有svgs.
我正在使用下面的函数来生成图像.
function svgToCanvas (targetElem) {
var nodesToRecover = [];
var nodesToRemove = [];
var svgElem = targetElem.find('svg');
svgElem.each(function(index, node) {
var parentNode = node.parentNode;
var svg = parentNode.innerHTML;
var canvas = document.createElement('canvas');
canvg(canvas, svg);
nodesToRecover.push({
parent: parentNode,
child: node
});
parentNode.removeChild(node);
nodesToRemove.push({
parent: parentNode,
child: canvas
});
parentNode.appendChild(canvas);
});
html2canvas(targetElem, {
allowTaint: true,
onrendered: function(canvas) {
// var ctx = canvas.getContext('2d');
// ctx.webkitImageSmoothingEnabled = false;
// ctx.mozImageSmoothingEnabled = false;
// ctx.imageSmoothingEnabled = false;
var img_PNG = Canvas2Image.saveAsPNG(canvas);
}
}); …Run Code Online (Sandbox Code Playgroud) function swap(x,y){
var t=x;
x=y;
y=t;
}
Run Code Online (Sandbox Code Playgroud)
这不行.当你swap(a,b),变量a和b被复制到功能,在该功能无论发生什么事不会影响真正的价值a和b.我想要这样的东西:
(function(){
a=1;
b=2;
function swap(){//something}
swap(a,b);
console.log(a) //2
console.log(b) //1
})()
Run Code Online (Sandbox Code Playgroud)
这该怎么做?
我有一个函数来描述我的问题:
function testingFunc(value) {
var temp = value;
console.log("temp before: " + JSON.stringify(temp));
console.log("arrayTest before: " + JSON.stringify(arrayTest));
temp.unshift(123);
console.log("temp after unshift: " + JSON.stringify(temp));
console.log("arrayTest after unshift: " + JSON.stringify(arrayTest));
temp.push(456);
console.log("temp after push: " + JSON.stringify(temp));
console.log("arrayTest after push: " + JSON.stringify(arrayTest));
temp.shift();
console.log("temp after shift: " + JSON.stringify(temp));
console.log("arrayTest after shift: " + JSON.stringify(arrayTest));
temp.pop();
console.log("temp after pop: " + JSON.stringify(temp));
console.log("arrayTest after pop: " + JSON.stringify(arrayTest));
return temp;
}
var arrayTest = [1,2,3,4,5];
var arrayTestTwo;
arrayTestTwo …Run Code Online (Sandbox Code Playgroud)function bubbleSort(toSort) {
let sort = toSort;
let swapped = true;
while(swapped) {
swapped = false;
for(let i = 0; i < sort.length; i++) {
if(sort[i-1] > sort[i]) {
let temp = sort[i-1];
sort[i-1] = sort[i];
sort[i] = temp;
swapped = true;
}
}
}
return sort;
}
let asdf = [1,4,3,2];
let asd = bubbleSort(asdf);
console.log(asdf, asd);
Run Code Online (Sandbox Code Playgroud)
此代码的输出为:[1,2,3,4] [1,2,3,4].
我期待的是:[1,4,3,2] [1,2,3,4].
我想知道的是,为什么这会改变asdf变量?bubbleSort函数接受给定的数组(asdf),复制它(排序),然后处理该变量并返回它,asd设置为等于.我觉得自己像个白痴,但我不知道为什么会这样:(
假设我们有两个对象:
var a = { foo: { bar: 1 } }
var b = { foo: { bar: 2 } }
Run Code Online (Sandbox Code Playgroud)
如果我将对象 b 设置为 a ( a = b),我期望 a 采用 b 的值,而不是引用。所以,在这种情况下:
a = b
a.foo.bar = 3
console.log(b.foo.bar);
Run Code Online (Sandbox Code Playgroud)
我预计最后一个console.log显示的是 2,而不是 3。为什么?只是因为我更改了与 相关的属性a,而不是与b.
我不明白为什么 JavaScript 也会更改b属性以及如何避免这种意外行为。
如何避免这种行为?我应该以不同的方式将对象分配给变量吗?
这是代码
funtion App(){
const [Cards, setCards] = useState([
{id: 0 , name: 'Harry Potter'},
{id: 1 , name: 'Hermonie Granger'},
{id: 2 , name: 'Ron Weasly'},])
const shuffle = (arr)=>{
// just shuffle the arr then return it.
}
return (
<div className="App">
<div>{Cards[0].name}</div>
<button onClick={() => {
setCards([...shuffle(Cards)])
console.log(Cards)
}}>Testing</button>
</div>
);
}
Run Code Online (Sandbox Code Playgroud)
为什么这个有效setCards([...shuffle(Cards)])
,而不是这个setCards(shuffle(Cards))。
即使我不使用展开运算符,它也会洗牌(请参阅控制台),但不会在页面中显示它。
:) ;) ....
javascript ×9
arrays ×3
function ×2
jquery ×2
algorithm ×1
html ×1
html2canvas ×1
immutability ×1
mutable ×1
object ×1
oop ×1
react-hooks ×1
reactjs ×1
spread ×1
svg ×1
swap ×1