...每个对象还引用同一数组中的其他对象?
当我第一次想出这个问题时,我就是这样的
var clonedNodesArray = nodesArray.clone()
Run Code Online (Sandbox Code Playgroud)
将存在并搜索有关如何在javascript中克隆对象的信息.我确实在StackOverflow上找到了一个问题(由同样的@JohnResig回答)他指出用jQuery你可以做的
var clonedNodesArray = jQuery.extend({}, nodesArray);
Run Code Online (Sandbox Code Playgroud)
克隆一个对象.我试过这个,但这只复制了数组中对象的引用.所以,如果我
nodesArray[0].value = "red"
clonedNodesArray[0].value = "green"
Run Code Online (Sandbox Code Playgroud)
nodesArray [0]和clonedNodesArray [0]的值都将变为"绿色".然后我试了一下
var clonedNodesArray = jQuery.extend(true, {}, nodesArray);
Run Code Online (Sandbox Code Playgroud)
哪个深层复制了一个Object,但我分别从Firebug和Opera Dragonfly 那里得到了" 过多的递归 "和" 控制堆栈溢出 "的消息.
你会怎么做?这是不应该做的事吗?在Javascript中是否有可重用的方法?
我试图了解如何在 Google Apps 脚本中使用 javascript 原型和继承,但显然我仍然缺少一些东西。
这是我正在研究的结构。
function Father ( ) {
this.init.apply ( this, arguments );
}
Father.prototype ={
init: function ( ) {
var optns = arguments[0] || {};
this.job = optns.job || "unemployed";
this.cars = optns.cars || [];
}
}
function Son ( ) {
this.init.apply ( this, arguments );
}
Son.prototype = {
init : function ( ) {
Father.prototype.init.apply (this, arguments);
var optns = arguments[0] || {};
this.computers = optns.tablets || [];
}
}
Run Code Online (Sandbox Code Playgroud)
儿子与父亲的不同之处仅在于拥有电脑的财产。 …
我有一个阵列说
var list = ["first", "second"];
Run Code Online (Sandbox Code Playgroud)
现在我将列表分配给其他变量说
var temp = list;
Run Code Online (Sandbox Code Playgroud)
现在,当我使用拼接的名单像
list.splice(0,1);
Run Code Online (Sandbox Code Playgroud)
现在,当我检查它显示的列表的值时
list = ["second"]
Run Code Online (Sandbox Code Playgroud)
当我检查temp的值时,它说
temp = ["second"]
Run Code Online (Sandbox Code Playgroud)
我想知道为什么会这样?为什么temp的值会改变?
我确信这是一件愚蠢的事情,但我无法解决它。
如果我这样做:
var arr = [1, 2, 3, "up"];
var duplicate = arr;
duplicate[3] = "down";
console.log(arr[3]); //"down"
console.log(duplicate[3]); //"down"
Run Code Online (Sandbox Code Playgroud)
那么为什么原始数组也被修改了呢?这与它们指向同一个数组有关吗?
如何只修改重复项?
var alph = ["a", "b", "c"];
var r = [];
for(var i = 0; i < 5; i += 1) {
r.push(alph);
}
r[0].reverse();
console.log(r);
/* Output
[ [ 'c', 'b', 'a' ],
[ 'c', 'b', 'a' ],
[ 'c', 'b', 'a' ],
[ 'c', 'b', 'a' ],
[ 'c', 'b', 'a' ] ]
*/
/* Expected output
[ [ 'c', 'b', 'a' ],
[ 'a', 'b', 'c' ],
[ 'a', 'b', 'c' ],
[ 'a', 'b', 'c' ],
[ …Run Code Online (Sandbox Code Playgroud) var arr = [{a: "one", b: "two"}];
/* in real code I have actual filter condition, but the filtered result
share some common properties with value */
var res = {
arr1: arr.filter(x => x),
arr2: arr.filter(x => x)
};
res.arr1.forEach(x => x.a = "a");
console.log(arr); //should print [{a: "one", b: "two"}]
console.log(res.arr1); //should print [{a: "a", b: "two"}]
console.log(res.arr2); //should print [{a: "one", b: "two"}]Run Code Online (Sandbox Code Playgroud)
如果我更改了arr1对象数组中的值,res那么为什么更改应用于arr2和res?filter创建新数组然后不应用效果.
我在这做错了什么?
我创建了变量 bpJson,但我不明白它为什么会改变。我每 5 秒控制台记录一次,每次迭代都会改变。我只希望 babyPieData 改变而 bpJson 每次都保持不变。setPieOptions 中没有 bpJson,所以我没有包含该函数的代码。
var createVariance = function(bpJson, babyPieData){
var n = bpJson.length;
for ( var i = 0; i < n; i++){
var amount = Math.floor(Math.random() * 4);
var operator = Math.floor(Math.random() *2) + 1;
if (operator === 1){
babyPieData[i] = (bpJson[i] + amount);
if (babyPieData[i] > 100){
babyPieData[i] = 100;
}
}else{
babyPieData[i] = (bpJson[i] - amount);
if (babyPieData[i] < 0){
babyPieData[i] = 1;
}
}
setPieOptions(babyPieData);
}
};
var getBabyPieData = …Run Code Online (Sandbox Code Playgroud) 所以我在javascript和HTML5 Canvas中构建了一个圆形随机颜色选择器实用程序,所有组件都是动态的,对象的大小调整为屏幕大小,间距也根据屏幕大小调整.此外,如果用户调整显示大小,该实用程序也会动态调整大小.
我正在使用数组来存储圆圈的颜色.生成圆圈时,它们使用数组中的第一种颜色,从数组中删除该颜色,然后随机播放该数组.
问题是当用户调整显示器大小时,颜色数组没有足够的颜色来绘制所有圆圈,这是因为代码删除了使用的颜色,因此没有重复.但是,我尝试通过声明一个名为origColours的常量颜色数组并将colors数组设置为等于origColours数组来解决此问题.
以下是我编写的代码.我看不出origColours数组是如何或为什么被操纵的,希望你能提供帮助
:)
//########//SETUP
var canvas = document.getElementById("myCanvas");
var c = canvas.getContext("2d");
canvas.height = innerHeight;
canvas.width = innerWidth;
document.documentElement.style.overflow = 'hidden'; // firefox, chrome
document.body.scroll = "no"; // ie only
//########//COLORS
const origColours = ["#1c2133", "#2b6ea8", "#5d99bf", "#333968", "#000000", "#b000b0", "#0000aa", "#ff0000", "#00aaaa", "#7CFC00", "#00FF7F", "#8B0000", "#F0E68C"];
var colours = ["#1c2133", "#2b6ea8", "#5d99bf", "#333968", "#000000", "#b000b0", "#0000aa", "#ff0000", "#00aaaa", "#7CFC00", "#00FF7F", "#8B0000", "#F0E68C"];
//########//VARIABLES
var backgroundColour = 0;
var mouse = {
x: …Run Code Online (Sandbox Code Playgroud)