Code Monkey home page Code Monkey logo

Comments (4)

paulbartrum avatar paulbartrum commented on June 25, 2024 2

Don't use [JsField] -- it is not generally useful. Instead, you should use one of two approaches:

Simple property method

public class MyJsObjectInstance : ObjectInstance
{
    public MyJsObjectInstance(ObjectInstance prototype) : base(prototype)
    {
        // No need to call PopulateFunctions().
        MyString = "This is my string";
    }

    public string MyString
    {
        get { return this["myString"]; }
        set { this["myString"] = value; }
    }
}

Pros:

  • Reading and writing myString from Javascript is relatively fast.

Cons:

  • Setting the property in Javascript does not call the MyString setter.
  • Reading and writing MyString from .NET is slow.

Accessor property method

public class MyJsObjectInstance : ObjectInstance
{
    private string myString;

    public MyJsObjectInstance(ObjectInstance prototype) : base(prototype)
    {
        this.PopulateFunctions();
        this.myString = "This is my string";
    }

    [JsProperty(Name = "myString", IsEnumerable = true)]
    public string MyString
    {
        get { return this.myString; }
        set { this.myString = value; }
    }
}

Pros:

  • Reading and writing MyString from .NET is fast.
  • Setting the property in JavaScript does call the MyString setter.

Cons:

  • Reading and writing myString from JavaScript is slow.

from jurassic.

paulbartrum avatar paulbartrum commented on June 25, 2024 1

By the way, the behaviour with JSON.stringify is not a bug. JSON.stringify simply does not serialize properties which are marked as non-enumerable. If you want a property to appear in the serialized output, it needs to be enumerable.

from jurassic.

DAVco4 avatar DAVco4 commented on June 25, 2024

Hi Paul, thanks for the response.

I'm afraid that I was only able to get the first method working (simple property method) and showing up in the output of JSON.stringify - the second method does not seem to work despite the IsEnumerable parameter being set.

Performing propertyIsEnumerable() on an instance of the object returns true for the first method, but false for the second - so I think the IsEnumerable flag is being ignored for some reason.

public class MyJsObjectInstance : ObjectInstance
{

    private string mySecondString;

    public MyJsObjectInstance(ObjectInstance prototype)
        : base(prototype)
    {
        this.PopulateFunctions();
        this.PopulateFields();
    }

    public MyJsObjectInstance(ObjectInstance prototype, int i)
        : base(prototype)
    {
        this.MyFirstString = "my first string";
        this.MySecondString = "my second string";
    }

    // Works fine
    public string MyFirstString
    {
        get { return (string)this["myFirstString"]; }
        set { this["myFirstString"] = value; }
    }

    // Does not show up in JSON.stringify()
    [JSProperty(Name = "mySecondString", IsEnumerable = true)]
    public string MySecondString
    {
        get { return this.mySecondString; }
        set { this.mySecondString = value; }
    }
}

from jurassic.

paulbartrum avatar paulbartrum commented on June 25, 2024

Oh, that's because JSON.stringify doesn't serialize properties in the prototype. mySecondString is in the prototype but myFirstString is not; it's set directly on the object.

Try this:

console.log(JSON.stringify(myJsObject));  // doesn't show mySecondString
console.log(JSON.stringify(Object.getPrototypeOf(myJsObject)));  // does show mySecondString

I checked, and according to the spec this behaviour (not serializing properties in the prototype) is correct.

from jurassic.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.