JavaScript 中指向变量的指针

发布于 2024-12-08 19:05:03 字数 456 浏览 1 评论 0原文

有没有办法在 JavaScript 中创建\返回一个指向变量的指针?
就像,在 PHP 中:

function func() {
    .....
    return &$result;
}

我有一个 JS 函数,例如:

function(...) {
    x=document.getElements..... //x is HTML tag
    if (x.localName=='input') return x.value //String variable
    else if (x.localName=='textarea') return x.innerHTML //String variable
}

使用这个函数,我可以从 x 获取数据的副本,但不能更改源。
我也希望有机会改变它。

谢谢

Is there any way to create\return a pointer to variable in JavaScript ?
like, in PHP :

function func() {
    .....
    return &$result;
}

I have a JS function like:

function(...) {
    x=document.getElements..... //x is HTML tag
    if (x.localName=='input') return x.value //String variable
    else if (x.localName=='textarea') return x.innerHTML //String variable
}

Using this function, I can get a copy of the data from x, but not to change the source.
I want possibility to change it too.

Thanks

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

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

发布评论

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

评论(4

扛起拖把扫天下 2024-12-15 19:05:03

不,您不能返回指向字符串的指针。在 Javascript 中,对象通过引用自动分配和传递,并且原语被复制。因此,如果您return x;,那么您可以修改x.innerHTML,但如果您返回x.innerHTML,则字符串将被复制。

No, you can't return a pointer to a string. In Javascript Objects are assigned and passed by reference automatically, and primitives are copied. So if you return x; then you can modify x.innerHTML, but if you return x.innerHTML the string will be copied.

烙印 2024-12-15 19:05:03

Hej Dani-Br

您可以执行类似的操作,

function(...) {
 var x = document.getElements..... //x is HTML tag
 if (x.localName=='input') return {
   get: function() { return x.value },
   set: function(v) { x.value = v; }
 };
 else if (x.localName=='textarea') return {
  get: function() { return x.innerHTML },
  set: function(v) { x.innerHTML = v; }
 };
}

以便调用者可以设置并获取该值。
附:使用 var 声明局部变量

GL

Hej Dani-Br

you can do something like this

function(...) {
 var x = document.getElements..... //x is HTML tag
 if (x.localName=='input') return {
   get: function() { return x.value },
   set: function(v) { x.value = v; }
 };
 else if (x.localName=='textarea') return {
  get: function() { return x.innerHTML },
  set: function(v) { x.innerHTML = v; }
 };
}

so that the caller can set and get the value.
ps. use var to declare local vars

GL

允世 2024-12-15 19:05:03

基本类型按值传递。对象通过引用传递。我猜 x.innerHTML 是一个字符串,所以它是按值传递的。

看一下这段代码,它显示了通过引用传递对象:

function test(str){
 return str;
}

var k = { txt : 'foo' },
    n = test(k);

console.log(n.txt);
k.txt = 'bar';
console.log(n.txt);

Primitive types are passed by value. Objects are passed by reference. I guess x.innerHTML is a string, so it is passed by value.

Take a look at this piece of code, it shows you passing objects by reference:

function test(str){
 return str;
}

var k = { txt : 'foo' },
    n = test(k);

console.log(n.txt);
k.txt = 'bar';
console.log(n.txt);
魔法少女 2024-12-15 19:05:03

这可能不完全是您正在寻找的,但尚未提出的一件事是内部函数可以访问其外部函数的变量:

function myFunc(){
    var x = "hello";
    myInnerFunc();  

    function myInnerFunc (){
        //shows the variable of the outer function
        console.log(x);
    };

};

此外,您可以使用闭包来保存外部函数变量的状态:

function myFunc2(){
    var x = "hello";
    var timesCalled = 0;

    return function (){
        timesCalled++;
        console.log(x + " called: " + timesCalled);
    };

};

var fun = myFunc2();

fun(); //1
fun(); //2
fun(); //3

This might not be exactly what you are looking for but one thing that hasn't been brought up yet is that inner functions can access the variables of their outer functions:

function myFunc(){
    var x = "hello";
    myInnerFunc();  

    function myInnerFunc (){
        //shows the variable of the outer function
        console.log(x);
    };

};

Additionally you can use a closure to save the state of an outer functions variables:

function myFunc2(){
    var x = "hello";
    var timesCalled = 0;

    return function (){
        timesCalled++;
        console.log(x + " called: " + timesCalled);
    };

};

var fun = myFunc2();

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