我想将一个对象或数组传递给一个函数,使其未定义,并在函数执行结束后查看更改.
var arr = ['aaa', 'bbb', 'ccc'];
var reset = function (param) {
param[0] = 'bbb';
param = undefined;
}
reset(arr);
Run Code Online (Sandbox Code Playgroud)
好的,结果是['bbb', 'bbb', 'ccc'],但我希望它是undefined.是否有可能有效地做到这一点?
我有一个函数来描述我的问题:
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)我只是无法理解变量是如何传递的,为什么有些是通过引用传递而另一些是通过值传递的?
例:
var a=4;
var b=a;
b=b++;
alert(a);//unmodified 4
var c=["t","ttt"];
var d=c;
d=d.sort(function(x,y){return (y.length-x.length);});
alert(c);//modified ["ttt","t"]
Run Code Online (Sandbox Code Playgroud)
我在哪里可以找到哪些变量可以像第一个例子一样工作,哪个像第二个?(布尔,字符串等...有太多的东西可以通过恶误来测试它们)
嘿家伙我知道存在,.slideToggle()但我想稍后添加更多功能.
我不知道我做错了什么,滑动工作,但我不能滑下来.
我可以不覆盖我的var吗?当有人可以帮助我时会很好.
$(document).ready(function () {
var resizeValue = true;
$(".resizeSelect").click(function () {
if (resizeValue === true) {
$(".resize").slideUp(
function () {
$('.parent').height($('.child').height('100'));
});
var resizeValue = false;
} else {
$(".resize").slideDown(
function () {
$('.parent').height($('.child').height('100'));
});
var resizeValue = true
};
});
});
Run Code Online (Sandbox Code Playgroud) 对象通过引用传递。它们永远不会被复制。我有如下代码段:
var person={firstname:'John', lastname:'Smith'}
var anotherPerson=person
anotherPerson.nickname='Curly'
console.log(person.nickname)
"Curly"
var fname=person.firstname
console.log(fname)
"John"
person.firstname = 'Tom'
console.log(anotherPerson)
Object {firstname: "Tom", lastname: "Smith", nickname: "Curly"}
console.log(fname)
"John" <-- fname is not updated
Run Code Online (Sandbox Code Playgroud)
我的问题是在我将对象人的名字更新为“Tom”之后,为什么本地变量 fname 没有更新?
我有一个人的代码,用于设置新对象的名称.
function setName(human) {
human.name = "Nicholas";
human = new Object();
human.name = "Greg";
};
var newPerson = new Object();
setName(newPerson);
console.log(newPerson.name);
Run Code Online (Sandbox Code Playgroud)
我想使用person对象的属性'name'的值.
我期待价值是'格雷格',但我得到'尼古拉斯'.
这是我的理解:'human'是指向Object的指针.调用该函数时,将通过newPerson的名称创建指向同一Object的另一个指针.
newPerson对象的'name'属性将设置为'Nicholas'.然后,在函数的第二行中创建另一个newPerson对象.因此,应该销毁第一个实例.然后,此新Object的'name'属性将设置为'Greg'.我哪里错了?
在其他编程语言中,我们使用&关键字通过引用传递变量.
例如,在PHP;
$a = 10;
function something(&$a){
$a = 7;
};
something($a);
echo $a;
// 7
Run Code Online (Sandbox Code Playgroud)
我们怎样才能在javascript中执行此操作?
当用户点击向右或向左箭头时,我正试图获得下一个或上一个.图像按数组索引;
list: function (index) {
let items = this.images;
return {
next: function () {
if (index > items.length -1) {
index = 0;
}
return items[index++];
},
prev: function () {
if (index < 0) {
index = items.length -1;
}
return items[index--];
}
}
}
Run Code Online (Sandbox Code Playgroud)
在这个迭代器之外,我需要使用索引变量.但我只是得到旧的价值......我想得到当前的指数.
我对 Javascript 很陌生,并且有一个关于声明和访问变量的初学者问题。为什么 console.log 显示“text”的 textContent 已更改,但实际上并未更改?如果我不是在函数外部声明“文本”,而是在函数内部声明它,则可以使函数正常工作 - 然后实际的 textContent 发生变化
let text = document.querySelector("#text").textContent
function changeText() {
text = "Goodbye"
}
changeText()
console.log(text)Run Code Online (Sandbox Code Playgroud)
<p id="text">Hi</p>Run Code Online (Sandbox Code Playgroud)
我无法在下面的交换函数中获得所需的结果,我希望将值打印为 3,2
function swap(x,y){
var t = x;
x = y;
y = t;
}
console.log(swap(2,3));
Run Code Online (Sandbox Code Playgroud)
任何线索将不胜感激!