简体   繁体   English

在javascript中定义函数的参数列表

[英]define argument list for function in javascript

I'm sorry if this is a duplicate, but i really don't actually know how to even ask this question.如果这是重复的,我很抱歉,但我真的不知道如何问这个问题。 I'm coming from a native language background where you have control of how parameters are pass (by value, reference or pointer), and even though i've been using javascript for many years, i still have a tough time with knowing when variables are being passed by reference or value sometimes.我来自母语背景,您可以控制参数的传递方式(按值、引用或指针),尽管我已经使用 javascript 多年,但我仍然很难知道什么时候变量有时通过引用或值传递。

I'm would like to allow a function to be overridden in a javascript class, but basically i'm having a problem passing the arguments as a reference to the function.我想允许在 javascript 类中覆盖一个函数,但基本上我在将参数作为对函数的引用传递时遇到了问题。

I'll have to provide an example because i don't know exactly how to explain this.我将不得不提供一个例子,因为我不知道如何解释这一点。

So i have a "class", i'll call it exampleClass:所以我有一个“类”,我称之为exampleClass:

exampleClass = function()
{
    var m_largearray = [];
    var m_width = 100;
    var m_funcParams = {data: m_largearray, width:m_width};
    var m_someFunction = function(_params){
        // do stuff
    }
    var m_finalValues = [];
    return {
        doProcessing: function()
        {
            // do some things
            m_finalValues = m_someFunction(m_funcParams);
            // do stuff with the m_finalValues
        }
        setFunction: function(_func, _params)
        {
            m_someFunction = _func;
            m_funcParams = _params;
        },
        largearray: m_largearray,
        width: m_width
    }
}

So this class has a function which gets called, doProcessing().所以这个类有一个被调用的函数,doProcessing()。 Inside this function, it calls another function that does part of the processing, but i would like to have control of this part of the processing outside the class when i do not want the default way to process the data.在这个函数内部,它调用另一个执行部分处理的函数,但是当我不希望以默认方式处理数据时,我希望在类之外控制这部分处理。

So something like this:所以像这样:

var classinstance = exampleClass();
var somespecialarray = [];
var someint = 5;
var somewidth = 900;
classinstance.setFunction(function(_params){
    // do some processing with different set of params
    // even though somewidth was set to 400, i will still get _params.width = 900 here
    // return some array
},
{data:somespecialarray, anint:someint, width:somewidth});
somewidth = 400;
classinstance.doProcessing();

This is just a complete example of what i'm trying to do, i just wrote this code here off the top of my head so if theres errors thats why.这只是我正在尝试做的事情的一个完整示例,我只是在这里写了这段代码,所以如果有错误,那就是原因。

Anyway, the problem with this is that the parameter list i set (i've tried using an array instead of an object).无论如何,问题在于我设置的参数列表(我尝试使用数组而不是对象)。 When i set m_funcParams, it seems the data is copied rather than referenced.当我设置 m_funcParams 时,数据似乎被复制而不是被引用。 When i change these arguments, m_largearray for example, when the default function gets called, the parameter data is what it was when i originally set m_funcParams, and not what i have updated m_largearray to be, the same as when i override the function, if i change somewidth from 900 to 800, when i call the function, the width parameter i still 900.当我更改这些参数时,例如 m_largearray,当调用默认函数时,参数数据是我最初设置 m_funcParams 时的数据,而不是我将 m_largearray 更新为的数据,与我覆盖函数时相同,如果我将 somewidth 从 900 更改为 800,当我调用该函数时,宽度参数仍为 900。

I hope this makes sense, i just need some clarity on why the function is not getting the updated values, and if there is a better way of doing this.我希望这是有道理的,我只需要弄清楚为什么函数没有获得更新的值,以及是否有更好的方法来做到这一点。

So, why is the function not getting the changed values, and is there a better way of doing this?那么,为什么函数没有得到改变的值,有没有更好的方法来做到这一点?

EDIT: Here is a complete working example.编辑:这是一个完整的工作示例。 I think i just realized when i set the variable to be something else, i'm basically losing a "pointer" to the value it was before.我想我刚刚意识到当我将变量设置为其他东西时,我基本上失去了一个指向它之前的值的“指针”。 if i set an element in an array, it updates in the function, but if i set the whole array, it does not如果我在数组中设置一个元素,它会在函数中更新,但如果我设置整个数组,它不会

<html>
<body>
<script>
var exampleClass = function()
{
    var m_largearray = [1,2,3];
    var m_width = 100;
    var m_funcParams = {data: m_largearray, width:m_width};
    var m_someFunction = function(_params){
        // do stuff
        alert(_params.data[0]);
    }
    var m_finalValues = [];
    return {
        doProcessing: function()
        {
            // do some things
            m_finalValues = m_someFunction(m_funcParams);
            // do stuff with the m_finalValues
        },
        setFunction: function(_func, _params)
        {
            m_someFunction = _func;
            m_funcParams = _params;
        },
        largearray: m_largearray,
        width: m_width
    }
}

var classinstance = exampleClass();
classinstance.largearray[0] = 3;
classinstance.doProcessing();

classinstance.largearray = [7,8,9];
classinstance.doProcessing();

var somespecialarray = [4,5,6];
var someint = 5;
var somewidth = 900;
classinstance.setFunction(function(_params){
    // do some processing with different set of params
    // even though somewidth was set to 400, i will still get _params.width = 900 here
    alert(_params.width);
    // return some array
},
{data:somespecialarray, anint:someint, width:somewidth});
somewidth = 400;
classinstance.doProcessing();
</script>
</body>
</html>

I hope I understood correctly and that this helps.我希望我理解正确,这会有所帮助。

First of all when you pass an object to a function it passes as a reference, so changing one of it's properties will change the referred object's property.首先,当您将对象传递给作为引用传递的函数时,因此更改其中一个属性将更改引用对象的属性。

However, when you invoked classinstance.setFunction in your example, you passed an object with a property width which recieved the variable somewidth , which is not an object so it gets passed by value.但是,当您在示例中调用classinstance.setFunction时,您传递了一个具有属性width的对象,该对象接收了变量somewidth ,它不是一个对象,因此它是按值传递的。

Finally, when you changed someWidth to 800 you only changed someWidth , because width was passed as a value.最后,当您将someWidth更改为 800 时,您只更改了someWidth ,因为width是作为值传递的。

Edit: In addition, when invoking classinstance.setFunction , you passed a function with param _param which is not part of that scope.编辑:此外,在调用classinstance.setFunction ,您传递了一个不属于该范围的参数为_param的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM