B4J Question [BANano] is BANanoObject.HasOwnProperty equivalent to Map.ContainsKey?

alwaysbusy

Expert
Licensed User
Longtime User
The hasOwnProperty() method returns a boolean indicating whether the object has the specified property as its own property (as opposed to inheriting it). So it can return false, but still have that property. It just is inherited from another object and not its own.

You can use GetField() and check if it is null or BANano.UNDEFINED to see if the object actually has the property.

Alwaysbusy
 
Upvote 0

Mashiane

Expert
Licensed User
Longtime User
Just to be sure,, if

B4X:
dim xx as BANanoObject
xx.Initialize5
xx.SetField("a", "another")

if I run

B4X:
Log(xx.HasOwnProperty("a"))

then the result WILL BE True?

Update:

tests return true - thats good enough for me!

B4X:
Dim xx As BANanoObject
    xx.initialize5
    xx.SetField("a", "another")
    Dim b As Boolean = xx.RunMethod("hasOwnProperty", Array("a"))
    Log(b)
    '
    xx.SetField("c", BANano.UNDEFINED)
    Dim c As Boolean = xx.RunMethod("hasOwnProperty", "c")
    Log(c)
 
Last edited:
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
BANanoObject has its own method HasOwnProperty, so no need to use RunMethod.

B4X:
xx.HasOwnProperty

EDIT: reported as returning an error sand will be fixed in the next update.

But as said, not the right method to use as it can give unexpected results on more complex objects that inherit from another. Much safer to just check if the value returned from .GetField() is null or undefined. May work in your specific case here but chances you will forget its special purpose are real in the future and think it is a bug.

Alwaysbusy
 
Last edited:
Upvote 0
Top