简体   繁体   English

替代在javascript中通过引用传递

[英]Alternative to pass by reference in javascript

I'm a c++ programmer learning javascript. 我是一个学习javascript的c ++程序员。

I have a javascript function loadTexture to load an image from a URL and create an opengl texture from it. 我有一个javascript函数loadTexture来从URL加载图像并从中创建一个opengl纹理。 It works asynchronously and returns a Promise object so that I can tell when it's finished. 它以异步方式工作并返回一个Promise对象,以便我可以判断它何时完成。 All good :) 都好 :)

But I'd like to be able to tell it where to store the resulting opengl texture id, something like this - 但是我希望能够告诉它在哪里存储生成的opengl纹理id,就像这样 -

var catImageFuture = loadTexture("cat.png", this.catTextureId);
/* Wait for the future, and several other ones to complete before doing the next thing */

And have it store the opengl id it generates in the variable I passed in as the second parameters. 并将它在我传入的变量中生成的opengl id存储为第二个参数。 In C++ this would be simple, I could pass a reference or a pointer to the variable to the function. 在C ++中,这很简单,我可以将引用或指向变量的指针传递给函数。 But this isn't possible in Javascript as it has no pass by reference or pointers. 但这在Javascript中是不可能的,因为它没有通过引用或指针传递。

What is the right way to achieve a similar aim in Javascript. 在Javascript中实现类似目标的正确方法是什么。

JavaScript has no mechanism (yet) to elegantly inline asynchronous code. JavaScript没有机制(尚未)优雅地​​内联异步代码。 Right now, everything is done with callbacks. 现在,一切都是通过回调来完成的。 If you implement a done method on your Future object that passes its result as an argument to a callback, you'd be able to write: 如果在Future对象上实现了一个done方法,将其结果作为参数传递给回调,那么您将能够编写:

var that = this; // You may need this, depending on how you implement Future.done

loadTexture("cat.png").done(function(result) {
    that.catTextureId = result;
});

JavaScript runtimes will support generators in a few years, so you'll soon be writing: JavaScript运行时将在几年内支持生成器,因此您很快就会写:

this.catTextureId = yield loadTexture("cat.png");

In Javascript you would use a function. 在Javascript中,您将使用一个函数。 The function would receive the data as a parameter and store it wherever is appropriate. 该函数将接收数据作为参数并将其存储在适当的位置。 You might use a closure to capture a reference to a local variable. 您可以使用闭包来捕获对局部变量的引用。

Another option is to always wrap the data in an object. 另一种选择是始终将数据包装在对象中。 Pass the object to your function, and ithe function can assign a property of the object. 将对象传递给函数,函数可以赋值对象的属性。 Objects and arrays are passed like pointers in Javascript, so when the function modifies it, it will be visible to the caller. 对象和数组像Javascript中的指针一样传递,因此当函数修改它时,它将对调用者可见。

There are several ways: 有几种方法:

You can use custom object and manage that, like: 您可以使用自定义对象并对其进行管理,例如:

var cat = new Cat();
cat.texture = new Texture();
...

Or you can use callbacks: 或者您可以使用回调:

var TextureId;

function loadTexture(path, callback)
{
    var img = new Image(path);
    var id = (GetSomeHowId);\
    img.onload = (function(i){ return (function(){callback(i);}); })(id);
}

loadTexture("cat.png", (function(id){ TextureId = id; }));

Hope you got the point. 希望你明白这一点。

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

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