Browse Source

Added (*Object).Delete() method. See #205

Dmitry Panov 5 years ago
parent
commit
8ce18d86df
2 changed files with 18 additions and 0 deletions
  1. 12 0
      object_test.go
  2. 6 0
      value.go

+ 12 - 0
object_test.go

@@ -1,6 +1,7 @@
 package goja
 package goja
 
 
 import (
 import (
+	"fmt"
 	"reflect"
 	"reflect"
 	"testing"
 	"testing"
 )
 )
@@ -288,6 +289,17 @@ func TestExportToWrappedMapCustom(t *testing.T) {
 	}
 	}
 }
 }
 
 
+func ExampleObject_Delete() {
+	vm := New()
+	obj := vm.NewObject()
+	_ = obj.Set("test", true)
+	before := obj.Get("test")
+	_ = obj.Delete("test")
+	after := obj.Get("test")
+	fmt.Printf("before: %v, after: %v", before, after)
+	// Output: before: true, after: <nil>
+}
+
 func BenchmarkPut(b *testing.B) {
 func BenchmarkPut(b *testing.B) {
 	v := &Object{}
 	v := &Object{}
 
 

+ 6 - 0
value.go

@@ -778,6 +778,12 @@ func (o *Object) Set(name string, value interface{}) error {
 	})
 	})
 }
 }
 
 
+func (o *Object) Delete(name string) error {
+	return tryFunc(func() {
+		o.self.deleteStr(unistring.NewFromString(name), true)
+	})
+}
+
 // MarshalJSON returns JSON representation of the Object. It is equivalent to JSON.stringify(o).
 // MarshalJSON returns JSON representation of the Object. It is equivalent to JSON.stringify(o).
 // Note, this implements json.Marshaler so that json.Marshal() can be used without the need to Export().
 // Note, this implements json.Marshaler so that json.Marshal() can be used without the need to Export().
 func (o *Object) MarshalJSON() ([]byte, error) {
 func (o *Object) MarshalJSON() ([]byte, error) {