通过传递函数引用来初始化变量

发布于 2024-12-18 05:03:46 字数 655 浏览 4 评论 0原文

我想知道如何通过将变量作为函数的引用传递来初始化变量。这是代码:

var carToy, trainToy;

function setToyValue(name, description, toy) {
    toy = new makeToy(name, description);
}

function makeToy(name, description) {
    this.name = name;
    this.description = description;
}

function start() {
    setToyValue("toy car", "red, 4 wheels", carToy);
    setToyValue("train car", "green, with 5 wagons", trainToy);
    console.log(carToy);
    console.log(trainToy);
}

start();

显然,最后, carToy 和 trainToy 仍然是未定义的......关于如何做到这一点有什么建议吗?这是小提琴 --> http://jsfiddle.net/Osoascam/8YMsz/

I'm wondering how could you initialize a variable by passing it as a reference on a function. Here is the code:

var carToy, trainToy;

function setToyValue(name, description, toy) {
    toy = new makeToy(name, description);
}

function makeToy(name, description) {
    this.name = name;
    this.description = description;
}

function start() {
    setToyValue("toy car", "red, 4 wheels", carToy);
    setToyValue("train car", "green, with 5 wagons", trainToy);
    console.log(carToy);
    console.log(trainToy);
}

start();

Obviously, at the end, carToy and trainToy still are undefined... Any suggestions on how to do it? Here is the fiddle --> http://jsfiddle.net/Osoascam/8YMsz/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听,心雨的声音 2024-12-25 05:03:46

JavaScript 行为通过引用进行传递的方式有所不同(有些不同)。基本上,JavaScript 中的所有内容都是按值传递的。如果您将一个对象传递给一个函数并更改您体内的某些属性,并且您可以访问该函数外部的这些更改的值,这使我们猜测它是通过引用传递的,但实际上对于对象变量的值是参考

您实际上无法使用 JavaScript 来实现此目的,但您可以遵循一些简单的解决方法。如果你知道 JavaScript 中的每个对象都是键值对字典的一种风格,并且所有变量都可以通过索引器访问。

在您的情况下,正如我所说,变量的值是一个引用,在它不会持续存在的情况下更改该值是毫无意义的。而且您还知道当前上下文 (this) 是全局,因此您可以通过索引器访问变量并按名称更改值。

唯一的区别是,您将函数名称作为字符串传递。

var carToy, trainToy;

function setToyValue(name, description, toy) {
    this[toy] = new makeToy(name, description);
}

function makeToy(name, description) {
    this.name = name;
    this.description = description;
}

function start() {
    setToyValue("toy car", "red, 4 wheels", "carToy");
    setToyValue("train car", "green, with 5 wagons", "trainToy");
    console.log(carToy);
    console.log(trainToy);
}

start();

在我看来,这是您能做的最好的事情。

JavaScript acts this pass by reference differently (somewhat). Basically everything is passed by value in JavaScript. If you pass an object to a function and change some property in you and you have access to those changed values outside that function, that makes us guess that it's passed by reference, but actually for objects the value of the variable is a reference.

You can't actually achieve this using JavaScript but there's simple workarounds you can follow. If you know that every object in JavaScript is a flavor of Dictionary of Key Value pair, and all variables can be accessed by indexers.

In your case, as i said the value of the variable is a reference and its pointless to change the value where it wouldn't persist. And you also know that your current context (this) is the global so you may access your variable through indexer and change the value by name.

The only difference is, you are passing the name of the function as string.

var carToy, trainToy;

function setToyValue(name, description, toy) {
    this[toy] = new makeToy(name, description);
}

function makeToy(name, description) {
    this.name = name;
    this.description = description;
}

function start() {
    setToyValue("toy car", "red, 4 wheels", "carToy");
    setToyValue("train car", "green, with 5 wagons", "trainToy");
    console.log(carToy);
    console.log(trainToy);
}

start();

That's the best you can do in my opinion.

风渺 2024-12-25 05:03:46

你不能那样做。 new 返回对其创建的新对象的引用,因此您应该从 setToyValue 返回该引用,并将其分配给 start() 中的变量。

var carToy, trainToy;

function setToyValue(name, description) {
    return new makeToy(name, description);
}

function makeToy(name, description) {
    this.name = name;
    this.description = description;
}

function start() {
    carToy = setToyValue("toy car", "red, 4 wheels");
    trainToy = setToyValue("train car", "green, with 5 wagons");
    console.log(carToy);
    console.log(trainToy);
}

start();

You cannot do it that way. new returns a reference to the new Object it's created, so you should return that from setToyValue, and assign it to a variable in start().

var carToy, trainToy;

function setToyValue(name, description) {
    return new makeToy(name, description);
}

function makeToy(name, description) {
    this.name = name;
    this.description = description;
}

function start() {
    carToy = setToyValue("toy car", "red, 4 wheels");
    trainToy = setToyValue("train car", "green, with 5 wagons");
    console.log(carToy);
    console.log(trainToy);
}

start();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文