robertkrimen / otto

A JavaScript interpreter in Go (golang)
http://godoc.org/github.com/robertkrimen/otto
MIT License
8.01k stars 584 forks source link

`let x=1` and `const x=1` don't work. #515

Open ahuigo opened 8 months ago

ahuigo commented 8 months ago
package m

import (
    "fmt"
    "testing"

    "github.com/robertkrimen/otto"
)

func TestEvalJs(t *testing.T) {
    vm := otto.New()
    vm.Set("body", `{"page":1}`)
    v, err := vm.Run(`
        let abc = 2 + 2;// Not work here
        console.log("The value of abc is " + abc); // 4
    `)
    if err != nil {
        fmt.Printf("%+v, val:%v", err, v)
    }
}

I find another lib which supports const/let: https://github.com/dop251/goja

chrislowth commented 8 months ago

Otto is an implementation of ECMA standard version ES5. "let" and "const" are not part of that standard. They were introduced in ES6.

You should use "var" instead.

On Fri, 22 Dec 2023, 04:09 Ahuigo, @.***> wrote:

package m

import ( "fmt" "testing"

"github.com/robertkrimen/otto" )

func TestEvalJs(t *testing.T) { vm := otto.New() vm.Set("body", {"page":1}) v, err := vm.Run( let abc = 2 + 2;// Not work here console.log("The value of abc is " + abc); // 4 ) if err != nil { fmt.Printf("%+v, val:%v", err, v) } }

— Reply to this email directly, view it on GitHub https://github.com/robertkrimen/otto/issues/515, or unsubscribe https://github.com/notifications/unsubscribe-auth/AH2YHJJGJ6NK4IYEGGVF72LYKUBVRAVCNFSM6AAAAABA7JR6E6VHI2DSMVQWIX3LMV43ASLTON2WKOZSGA2TGMZVGI4TSMY . You are receiving this because you are subscribed to this thread.Message ID: @.***>

Asday commented 8 months ago

Please note that this is in the README also.

stevenh commented 8 months ago

PRs to introduce newer features are welcome, would be nice if we could get to ES6 in the future.