Ver código fonte

Fixed Readme for AssertFunction Call Case (#523)

* Fixed Readme for AssertFunction Call Case

* Made Consistent Both func Variable Name and Logic for Function Calling Examples
kudla 2 anos atrás
pai
commit
636fdf960d
1 arquivos alterados com 12 adições e 12 exclusões
  1. 12 12
      README.md

+ 12 - 12
README.md

@@ -21,7 +21,7 @@ Features
  * Capable of running Babel, Typescript compiler and pretty much anything written in ES5.
  * Sourcemaps.
  * Most of ES6 functionality, still work in progress, see https://github.com/dop251/goja/milestone/1?closed=1
- 
+
 Known incompatibilities and caveats
 -----------------------------------
 
@@ -77,7 +77,7 @@ FAQ
 
 ### How fast is it?
 
-Although it's faster than many scripting language implementations in Go I have seen 
+Although it's faster than many scripting language implementations in Go I have seen
 (for example it's 6-7 times faster than otto on average) it is not a
 replacement for V8 or SpiderMonkey or any other general-purpose JavaScript engine.
 You can find some benchmarks [here](https://github.com/dop251/goja/issues/2).
@@ -99,7 +99,7 @@ It gives you a much better control over execution environment so can be useful f
 ### Is it goroutine-safe?
 
 No. An instance of goja.Runtime can only be used by a single goroutine
-at a time. You can create as many instances of Runtime as you like but 
+at a time. You can create as many instances of Runtime as you like but
 it's not possible to pass object values between runtimes.
 
 ### Where is setTimeout()?
@@ -171,8 +171,8 @@ There are 2 approaches:
 - Using [AssertFunction()](https://pkg.go.dev/github.com/dop251/goja#AssertFunction):
 ```go
 const SCRIPT = `
-function f(param) {
-    return +param + 2;
+function sum(a, b) {
+    return +a + b;
 }
 `
 
@@ -181,12 +181,12 @@ _, err := vm.RunString(SCRIPT)
 if err != nil {
     panic(err)
 }
-sum, ok := AssertFunction(vm.Get("sum"))
+sum, ok := goja.AssertFunction(vm.Get("sum"))
 if !ok {
     panic("Not a function")
 }
 
-res, err := sum(Undefined(), vm.ToValue(40), vm.ToValue(2))
+res, err := sum(goja.Undefined(), vm.ToValue(40), vm.ToValue(2))
 if err != nil {
     panic(err)
 }
@@ -196,8 +196,8 @@ fmt.Println(res)
 - Using [Runtime.ExportTo()](https://pkg.go.dev/github.com/dop251/goja#Runtime.ExportTo):
 ```go
 const SCRIPT = `
-function f(param) {
-    return +param + 2;
+function sum(a, b) {
+    return +a + b;
 }
 `
 
@@ -207,13 +207,13 @@ if err != nil {
     panic(err)
 }
 
-var fn func(string) string
-err = vm.ExportTo(vm.Get("f"), &fn)
+var sum func(int, int) int
+err = vm.ExportTo(vm.Get("sum"), &sum)
 if err != nil {
     panic(err)
 }
 
-fmt.Println(fn("40")) // note, _this_ value in the function will be undefined.
+fmt.Println(sum(40, 2)) // note, _this_ value in the function will be undefined.
 // Output: 42
 ```