Javascript 修改JS对象';是否仅通过传递属性来设置属性?
我正在构建一个插件,它接受两个参数——高度和宽度。此插件的使用者可能有矩形对象、方形对象或多边形对象等。其宽度/高度的名称可能不同:Javascript 修改JS对象';是否仅通过传递属性来设置属性?,javascript,jquery,Javascript,Jquery,我正在构建一个插件,它接受两个参数——高度和宽度。此插件的使用者可能有矩形对象、方形对象或多边形对象等。其宽度/高度的名称可能不同: var square = { width: 200, height: 200 } var rectangle = { Width: 200, tall: 200 } var polygon = { w: 200, h: 200 } 我正在尝试构建一个函数,该函数接受宽度/高度参数并对其进行修改,以便更新父对象。大概是
var square = {
width: 200,
height: 200
}
var rectangle = {
Width: 200,
tall: 200
}
var polygon = {
w: 200,
h: 200
}
我正在尝试构建一个函数,该函数接受宽度/高度参数并对其进行修改,以便更新父对象。大概是这样的:
var alterStuff = function (width, height){
width = 200;
height = 200;
};
var obj = {width: "28", height: "34"};
alterStuff(obj.width, obj.height);
alert(obj.width);
alterStuff(rectangle.Width, rectangle.tall, function(Width, tall) {
rectangle.Width = Width;
rectangle.tall = tall;
});
目标是使用者可以传入两种基本类型,而altertstuff
函数将在对象上更新它们
可以使用什么模式来实现这一点?现在没有更新
obj
属性-我在警报中看到“28”,但我希望看到“200”而不传入对象,因为我不知道在所有情况下该对象上的属性名称 您必须传递对象本身,而不是其属性:
var alterStuff = function (obj){
obj.width = 200;
obj.height = 200;
};
var obj = {width: "28", height: "34"};
alterStuff(obj);
您可能需要阅读。您必须传递对象本身,而不是其属性:
var alterStuff = function (obj){
obj.width = 200;
obj.height = 200;
};
var obj = {width: "28", height: "34"};
alterStuff(obj);
你可能想读书。简短的回答:你不能 当你把某物传递给一个函数时,你就传递了它的值。如果是原语值,例如5,则将5传递给函数。如果它是一个对象,实际上它是对一个对象的引用,你传递那个引用。这意味着当您传递具有值(例如
45px
的obj.width
时,您只将该字符串传递给函数,并且绝对没有与obj
的连接
更新对象属性的唯一方法是传递对象本身和属性名称:
function change(obj, key, val) {
obj[key] = val;
}
var obj = { width : 5 };
change(obj, 'width', 10);
在特定情况下,可以尽最大努力从对象的键“猜测”属性名称。在下面的示例中,widthKey和heightKey是可选的,因此,如果库函数的用户有一个行为良好的对象,则不需要传递它们。否则,他应该通过考试
function updateSize(object, width, height, opt_widthKey, opt_heightKey) {
// potential width properties:
var widths = ['width', 'Width', 'w'];
// potential height properties:
var heights = ['height', 'Height', 'h'];
var widthKey = opt_widthKey || getPropertyName(object, widths);
if (!widthKey) throw new Error('Can not guess width property');
var heightKey = opt_heightKey || getPropertyName(object, heights);
if (!heightKey) throw new Error('Can not guess height property');
object[widthKey] = width;
object[heightKey] = height;
}
function getPropertyName(object, properties) {
for (var i=0; i<properties.length; i++) {
if (object.hasOwnProperty(properties[i]))
return properties[i];
}
}
// usage:
var obj = { width : 5, Height : 10 };
var notGood = { tall : 20, wide : 40 };
updateSize(obj, 100, 200);
updateSize(notGood, 100, 200, 'wide', 'tall');
函数更新大小(对象、宽度、高度、选择宽度键、选择高度键){
//潜在宽度属性:
变量宽度=['width','width','w'];
//潜在高度特性:
变量高度=['height','height','h'];
var widthKey=opt_widthKey | | getPropertyName(对象,宽度);
如果(!widthKey)抛出新错误('无法猜测宽度属性');
var heightKey=opt_heightKey | | getPropertyName(对象,高度);
如果(!heightKey)抛出新错误('无法猜测高度属性');
对象[widthKey]=宽度;
对象[高度键]=高度;
}
函数getPropertyName(对象、属性){
对于(var i=0;i简短回答:你不能
当你把某个东西传递给一个函数时,你传递它的值。如果它是一个原语值,例如5,你传递5给函数。如果它是一个对象,实际上它是一个对对象的引用,你传递那个引用。这意味着当你传递obj.width
这个值,例如45px
时,你只传递那个字符串to该功能完全没有连接到obj
更新对象属性的唯一方法是传递对象本身和属性名称:
function change(obj, key, val) {
obj[key] = val;
}
var obj = { width : 5 };
change(obj, 'width', 10);
在您的特定情况下,您可以尽最大努力从对象的键“猜测”属性名称。在下面的示例中,widthKey和heightKey是可选的,因此如果库函数的用户有一个行为良好的对象,他不需要传递它们。否则,他应该传递它们
function updateSize(object, width, height, opt_widthKey, opt_heightKey) {
// potential width properties:
var widths = ['width', 'Width', 'w'];
// potential height properties:
var heights = ['height', 'Height', 'h'];
var widthKey = opt_widthKey || getPropertyName(object, widths);
if (!widthKey) throw new Error('Can not guess width property');
var heightKey = opt_heightKey || getPropertyName(object, heights);
if (!heightKey) throw new Error('Can not guess height property');
object[widthKey] = width;
object[heightKey] = height;
}
function getPropertyName(object, properties) {
for (var i=0; i<properties.length; i++) {
if (object.hasOwnProperty(properties[i]))
return properties[i];
}
}
// usage:
var obj = { width : 5, Height : 10 };
var notGood = { tall : 20, wide : 40 };
updateSize(obj, 100, 200);
updateSize(notGood, 100, 200, 'wide', 'tall');
函数更新大小(对象、宽度、高度、选择宽度键、选择高度键){
//潜在宽度属性:
变量宽度=['width','width','w'];
//潜在高度特性:
变量高度=['height','height','h'];
var widthKey=opt_widthKey | | getPropertyName(对象,宽度);
如果(!widthKey)抛出新错误('无法猜测宽度属性');
var heightKey=opt_heightKey | | getPropertyName(对象,高度);
如果(!heightKey)抛出新错误('无法猜测高度属性');
对象[widthKey]=宽度;
对象[高度键]=高度;
}
函数getPropertyName(对象、属性){
对于(var i=0;i我认为在不传递对象的情况下,没有一个好方法可以完成您想要的操作。也许您可以返回一个由两个成员组成的数组,调用者将负责将其加载回正确的位置:
var alterStuff = function(width, height) {
return [+width * 2, +height * 2]; // for example
}
var obj = {width: "28", height: "34"};
var newValues = alterStuff(obj.width, obj.height);
obj.width = newValues[0];
obj.height = newValues[1];
或者,您可以让调用方提供回调函数:
var alterStuff = function(width, height, callback) {
// note: you may want to include some checks for undefined and that callback is a function
callback(+width *2, +height * 2);
}
alterStuff(obj.width, obj.height, function(width, height) {
obj.width = width;
obj.height = height;
});
所以现在如果我有:
var rectangle = {
Width: 200,
tall: 200
}
我可以这样调用alterStuff
:
var alterStuff = function (width, height){
width = 200;
height = 200;
};
var obj = {width: "28", height: "34"};
alterStuff(obj.width, obj.height);
alert(obj.width);
alterStuff(rectangle.Width, rectangle.tall, function(Width, tall) {
rectangle.Width = Width;
rectangle.tall = tall;
});
我为类似“宽度”和“高度”的属性选择的名称是调用者的选择
或者Tibos的回答是另一种选择,即在调用函数时传入键。我认为没有不传递对象的好方法来做您想做的事情。也许您可以返回一个由两个成员组成的数组,调用者将负责将其加载回正确的位置:
var alterStuff = function(width, height) {
return [+width * 2, +height * 2]; // for example
}
var obj = {width: "28", height: "34"};
var newValues = alterStuff(obj.width, obj.height);
obj.width = newValues[0];
obj.height = newValues[1];
或者,您可以让调用方提供回调函数:
var alterStuff = function(width, height, callback) {
// note: you may want to include some checks for undefined and that callback is a function
callback(+width *2, +height * 2);
}
alterStuff(obj.width, obj.height, function(width, height) {
obj.width = width;
obj.height = height;
});
所以现在如果我有:
var rectangle = {
Width: 200,
tall: 200
}
我可以这样调用alterStuff
:
var alterStuff = function (width, height){
width = 200;
height = 200;
};
var obj = {width: "28", height: "34"};
alterStuff(obj.width, obj.height);
alert(obj.width);
alterStuff(rectangle.Width, rectangle.tall, function(Width, tall) {
rectangle.Width = Width;
rectangle.tall = tall;
});
我为类似“宽度”和“高度”的属性选择的名称是调用者的选择
或者Tibos的答案是另一种选择,当你调用函数时,你可以输入键。你有一组固定的属性名,这里给出的三个,或者可能是任何东西吗?@chelmerich-可能是任何东西你有一组固定的属性名,这里给出的三个,或者可能是任何东西吗?@chelmerich-可能是任何东西