Quantcast
Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

New Post: Object.defineProperty on host object

$
0
0
Hello,

I am trying to use Object.defineProperty's set function to define a setter on a host object, however I remain unsuccessful in doing so. Getters interestingly enough work fine.

I add an instance of the following class to the engine as an host object using engine.AddHostObject("foo", new Foo()):
public class Foo 
{
    private string _bar = "Baz";

    public string Bar
    {
        get
        {
            return _bar;
        }
        set
        {
            _bar = value;
            Console.WriteLine(_bar);
        }
    }
}
I then define the getters and setters in JavaScript:
Object.defineProperty(foo, "bat", {
    set: function (f) {
        console.log("set: " + f);
        foo.Bar = f;
    },
    get: function () {
        return foo.Bar;
    }
});

console.log(foo.bat);
foo.bat = "quux";
console.log(foo.bat); outputs Baz while foo.bat = "quux"; displays an error with the following message:
Error: Object has no suitable property or field named 'bat'
    at Script Document:216:9 -> foo.bat = "quux";
Is there any way of getting this to work?

Viewing all articles
Browse latest Browse all 2297

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>