Can anyone explain to me in JavaScript language what "pointers" are?

huangapple go评论78阅读模式
英文:

Can anyone explain to me in JavaScript language what "pointers" are?

问题

我是一名学习Go语言的JavaScript程序员。我正在按照这个教程学习:http://tour.golang.org/#52

package main

import (
    "fmt"
    "math"
)

type Vertex struct {
    X, Y float64
}

func (v *Vertex) Abs() float64 {
    return math.Sqrt(v.X*v.X + v.Y*v.Y)
}

func main() {
    v := &Vertex{3, 4}
    fmt.Println(v.Abs())
}

我在维基百科和Go文档中读到了指针的定义,但我仍然无法理解它们。有人可以用JavaScript语言解释一下吗?

英文:

I'm a JavaScript coder who's learning Go. I'm following this tutorial: http://tour.golang.org/#52

package main

import (
    "fmt"
    "math"
)

type Vertex struct {
    X, Y float64
}

func (v *Vertex) Abs() float64 {
    return math.Sqrt(v.X*v.X + v.Y*v.Y)
}

func main() {
    v := &Vertex{3, 4}
    fmt.Println(v.Abs())
}

I read in Wikipedia and in the Go docs what pointers are, but I still can understand them. Can anyone explain them to me in JavaScript language?

答案1

得分: 6

它们在某种程度上类似于JS和其他语言中的对象引用,但并不完全相同。指针比引用更强大(因此更危险)。考虑以下JS代码。

var a = {foo: true};
var b = a;
a.foo = false;
console.log(b); // 输出: "Object { foo: false }"

这里的ab都像指针。当你执行b = a时,你并没有克隆一个对象,而是使b引用(或者说指向)与a相同的对象。在Go中,你可以同时做到这两点:

type T struct { Foo bool }

a := T{Foo: true}
b := a
a.Foo = false
fmt.Println(b) // b是一个副本,所以它没有改变。输出"{true}"。

pa := &T{Foo: true}
pb := pa
pa.Foo = false
fmt.Println(pb) // pb指向与pa相同的结构体,所以它输出"&{false}"

Playground

重要的区别是,在JS中,你实际上不能替换函数内部的对象。

var a = {foo: true};
(function(x) { x = {foo: false} })(a);
console.log(a); // 输出: "Object { foo: true }"

在Go中,你可以很好地做到这一点:

pa := &T{Foo: true}
func(p *T) { *p = T{Foo: false} }(pa)
fmt.Println(pa) // 输出: &{false}

Playground

另一个区别是,你可以创建指向任何类型(包括指针)的指针。

英文:

They are somewhat similar to object references in JS and other languages, but not quite. Pointer are more powerful (and thus, more dangerous) than references. Consider the following JS code.

var a = {foo: true};
var b = a;
a.foo = false;
console.log(b); // Output: "Object { foo: false }"

Both a and b here are like pointer. When you do b = a you don't clone an object, you make b refer (or point if you will) to the same object as a. In Go you can do both:

type T struct { Foo bool }

a := T{Foo: true}
b := a
a.Foo = false
fmt.Println(b) // b is a copy, so it didn't change. Prints "{true}".

pa := &T{Foo: true}
pb := pa
pa.Foo = false
fmt.Println(pb) // pb points to the same struct as pa, so it prints "&{false}"

Playground

The important difference is that in JS you can't actually replace the object inside a function.

var a = {foo: true};
(function(x) { x = {foo: false} })(a);
console.log(a); // Output: "Object { foo: true }"

In Go you can do it just fine:

pa := &T{Foo: true}
func(p *T) { *p = T{Foo: false} }(pa)
fmt.Println(pa) // Output: &{false}

Playground

Another difference is that you can make pointers to not just structs, but any type, including pointers.

答案2

得分: 0

一个老问题,但一个明显的观点是指针实际上是变量在内存中的地址。当通过指针传递变量时,实际上传递的是该变量所在的内存地址。

英文:

An old question but an obvious remark is that a pointer is actually the address of the variable in memory.
When passing a variable by pointer what is actually passed is the memory address where this variable is.

huangapple
  • 本文由 发表于 2014年10月18日 14:50:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/26437113.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定