Skip to content

Commit

Permalink
Merge pull request #511 from hswanepoel-godaddy/patch-1
Browse files Browse the repository at this point in the history
More consistent examples in readme
  • Loading branch information
dop251 authored May 31, 2023
2 parents 2352993 + 48c2f65 commit f5b4a75
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,14 @@ There are 2 approaches:

- Using [AssertFunction()](https://pkg.go.dev/github.com/dop251/goja#AssertFunction):
```go
vm := New()
_, err := vm.RunString(`
function sum(a, b) {
return a+b;
const SCRIPT = `
function f(param) {
return +param + 2;
}
`)
`

vm := goja.New()
_, err := vm.RunString(SCRIPT)
if err != nil {
panic(err)
}
Expand All @@ -199,7 +201,7 @@ function f(param) {
}
`

vm := New()
vm := goja.New()
_, err := vm.RunString(SCRIPT)
if err != nil {
panic(err)
Expand All @@ -225,7 +227,7 @@ the standard JavaScript naming convention, so if you need to make your JS code l
dealing with a 3rd party library, you can use a [FieldNameMapper](https://pkg.go.dev/github.com/dop251/goja#FieldNameMapper):

```go
vm := New()
vm := goja.New()
vm.SetFieldNameMapper(TagFieldNameMapper("json", true))
type S struct {
Field int `json:"field"`
Expand Down Expand Up @@ -257,7 +259,7 @@ Any exception thrown in JavaScript is returned as an error of type *Exception. I
by using the Value() method:

```go
vm := New()
vm := goja.New()
_, err := vm.RunString(`
throw("Test");
Expand All @@ -282,7 +284,7 @@ func Test() {
panic(vm.ToValue("Error"))
}

vm = New()
vm = goja.New()
vm.Set("Test", Test)
_, err := vm.RunString(`
Expand Down Expand Up @@ -313,7 +315,7 @@ func TestInterrupt(t *testing.T) {
}
`

vm := New()
vm := goja.New()
time.AfterFunc(200 * time.Millisecond, func() {
vm.Interrupt("halt")
})
Expand Down

0 comments on commit f5b4a75

Please sign in to comment.