I want to pass an object into a function as a parameter, and assign things to properties of that object, then those would be accessible outside of the function. But I am not to clear on how it is done.
我想将一个对象作为参数传递给函数,并将事物分配给该对象的属性,然后可以在函数外部访问它们。但我不清楚它是如何完成的。
At first I thought it could be done using callbacks, but it was made clear to me that JavaScript does not work that way. I eventually managed to get it working, and the undefined error messages went way. But I wanted to now pass an array through the function. How can this be done? This is what I've done so far, but it doesn't seem to be working.
起初我认为可以使用回调来完成,但我清楚地知道JavaScript不能以这种方式工作。我最终设法让它工作,未定义的错误消息开始了。但我现在想通过该函数传递一个数组。如何才能做到这一点?这是我到目前为止所做的,但它似乎没有起作用。
var journey = {};
journey['waypointsArrayItem'] = new Array();
setupAddress(journey)
function setupAddress(journey) {
var waypointsItem = $('.timeline-item.active-item > .timeline-status > .waypoints').text().substring(4);
journey.waypointsArrayItem = [];
journey.waypointsArrayItem = listToArray(waypointsItem, ', ');
for (var i = 0; i < waypointsArray.length; i++) {
journey.waypointsArrayItem[i] = journey.waypointsArrayItem[i];
}
}
2 个解决方案
#1
1
You are copying array items to themselves. Copy from the waypointsArray
array to the journey.waypointsArrayItem
array:
您正在将数组项复制到自己。从waypointsArray数组复制到journey.waypointsArrayItem数组:
function setupAddress(journey) {
var waypointsItem = $('.timeline-item.active-item > .timeline-status > .waypoints').text().substring(4);
var waypointsArray = listToArray(waypointsItem, ', ');
for (var i = 0; i < waypointsArray.length; i++) {
journey.waypointsArrayItem[i] = waypointsArray[i];
}
}
Depending on what you need, you could also just replace the entire array:
根据您的需要,您也可以只替换整个阵列:
function setupAddress(journey) {
var waypointsItem = $('.timeline-item.active-item > .timeline-status > .waypoints').text().substring(4);
journey.waypointsArrayItem = listToArray(waypointsItem, ', ')
}
#2
1
Assigning properties to an object in a function is the same as assigning properties outside of a function.
将属性分配给函数中的对象与分配函数外部的属性相同。
function addName(obj, name) {
obj.name = name;
}
var bob = {};
console.log(bob.name); // undefined
addName(bob, 'Bob');
console.log(bob.name); // 'Bob'
#1
1
You are copying array items to themselves. Copy from the waypointsArray
array to the journey.waypointsArrayItem
array:
您正在将数组项复制到自己。从waypointsArray数组复制到journey.waypointsArrayItem数组:
function setupAddress(journey) {
var waypointsItem = $('.timeline-item.active-item > .timeline-status > .waypoints').text().substring(4);
var waypointsArray = listToArray(waypointsItem, ', ');
for (var i = 0; i < waypointsArray.length; i++) {
journey.waypointsArrayItem[i] = waypointsArray[i];
}
}
Depending on what you need, you could also just replace the entire array:
根据您的需要,您也可以只替换整个阵列:
function setupAddress(journey) {
var waypointsItem = $('.timeline-item.active-item > .timeline-status > .waypoints').text().substring(4);
journey.waypointsArrayItem = listToArray(waypointsItem, ', ')
}
#2
1
Assigning properties to an object in a function is the same as assigning properties outside of a function.
将属性分配给函数中的对象与分配函数外部的属性相同。
function addName(obj, name) {
obj.name = name;
}
var bob = {};
console.log(bob.name); // undefined
addName(bob, 'Bob');
console.log(bob.name); // 'Bob'