Javascript对象作为参数处理(Javascript Object as Parameter Handling)

系统教程 行业动态 更新时间:2024-06-14 16:57:39
Javascript对象作为参数处理(Javascript Object as Parameter Handling)

我有2个Javascript对象,Limits和Wind,并且试图在一个对象上调用该函数,该对象是另一个对象的参数。 永远不会调用警报,控制台中没有错误。 我这样做了吗?

var Limits = { minx: 0, miny: 0, SetMaxLimits: function (x1, x2, y1, y2, zoom) { alert('j'); } }; var Wind = { Data : Limits, Screen : Limits, Rotation: 0, CanvasW : 0, CanvasH : 0, North : true, _Math : true, NormalLimits: function (x,y) { Data.SetMaxlimits(0, 0, 0, 0, 0); } }; Wind.NormalLimits(0,0);

I have 2 Javascript objects, Limits and Wind, and are trying to call the function on the one object that is a parameter of the other object. The alert is never called and there's no errors in the console. Am I doing this right?

var Limits = { minx: 0, miny: 0, SetMaxLimits: function (x1, x2, y1, y2, zoom) { alert('j'); } }; var Wind = { Data : Limits, Screen : Limits, Rotation: 0, CanvasW : 0, CanvasH : 0, North : true, _Math : true, NormalLimits: function (x,y) { Data.SetMaxlimits(0, 0, 0, 0, 0); } }; Wind.NormalLimits(0,0);

最满意答案

你有一个拼写错误,它是SetMaxLimits ,而不是SetMaxlimits ,你必须引用Data方法属性的对象,在这种情况下可以用this来完成,具体取决于函数的调用方式。

var Limits = { minx: 0, miny: 0, SetMaxLimits: function (x1, x2, y1, y2, zoom) { alert('j'); } }; var Wind = { Data : Limits, Screen : Limits, Rotation: 0, CanvasW : 0, CanvasH : 0, North : true, _Math : true, NormalLimits: function () { this.Data.SetMaxLimits(0, 0, 0, 0, 0); } }; Wind.NormalLimits();

小提琴

You have a typo, it's SetMaxLimits, not SetMaxlimits, and you have to reference the object that the Data method is a property of, which in this case could probably be done with this, depending on how the function is called.

var Limits = { minx: 0, miny: 0, SetMaxLimits: function (x1, x2, y1, y2, zoom) { alert('j'); } }; var Wind = { Data : Limits, Screen : Limits, Rotation: 0, CanvasW : 0, CanvasH : 0, North : true, _Math : true, NormalLimits: function () { this.Data.SetMaxLimits(0, 0, 0, 0, 0); } }; Wind.NormalLimits();

FIDDLE

更多推荐

本文发布于:2023-04-13 11:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/bae450398043edf2ab630fdc80ffe0b5.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   参数   Javascript   Handling   Parameter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!