Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Declare Private member variables? #115

Open
slow-typer opened this issue Oct 30, 2018 · 6 comments
Open

Declare Private member variables? #115

slow-typer opened this issue Oct 30, 2018 · 6 comments
Assignees

Comments

@slow-typer
Copy link

Thank you for this program!

Here is my environment:

  • [3.1.3 ] System Designer version number
  • [MacOS] System Designer platform
  • [10.14] Your OS version

How do I represent a private member in the class diagram using System-Designer? Is there support for private member variable declarations?

Recent searches for javascript private members show results that state to use "#" as a prefix for a name in javascript. But I don't know how to do this with JSON schema or JSON.

I can add a property to the Model such as the following:

"myParameter": {
"description": "",
"type": "string",
"readOnly": false,
"mandatory": false,
"private": true,
"default": ""
}

But, on the UML class diagram, it still shows "myParameter" with a "+" prefix. Is there an entry (or way) to make it show with a "-" prefix in the UML class diagram?

Any help would be greatly appreciated.

Thanks!

@ecarriou
Copy link
Member

ecarriou commented Nov 1, 2018

Actually there is no support for private member or method. I tried to stay very close to the JavaScript native language when I started to develop System Designer, this is why I did not add this feature. But you are right, private member will come soon in JavaScript, moreover when can simulate private member in JavaScript (with closure).

In fact we can add this feature, I just need to think a bit about that. But basically;

  • you will just need to add, for example, "visibility":"private" in the JSON of the model to set a property or a method as private,
  • this property/method will be shown with a - in UML class diagram,
  • autocomplete will not propose this property/method outside of the class and
  • use of this private property outside of the class will be forbidden at runtime. You will have an error message.

I will check soon on how to implement that feature.

And thanks for your support!

@ecarriou ecarriou self-assigned this Nov 1, 2018
@matthewkastor
Copy link

See Douglas Crockford's excellent information on JavaScript at https://crockford.com/javascript/

The page that is relevant to this issue is at https://crockford.com/javascript/private.html

@design-first design-first deleted a comment from cookaarongMD Nov 20, 2018
@design-first design-first deleted a comment from cookaarongMD Nov 20, 2018
@design-first design-first deleted a comment from cookaarongMD Nov 20, 2018
@ecarriou
Copy link
Member

Thanks for the links.

In fact, in System Designer systems are generated in a specific way: all the models and methods are generated in memory (no code is generated), each method is executed in their own context (in the JavaScript VM), so they can not have access to the parent context.

So I had to find another way (maybe with injections).

@matthewkastor
Copy link

matthewkastor commented Dec 16, 2018

So you're saying that the object methods are not part of the object definition? That doesn't make sense. At any rate Function.prototype.bind can bind any method to any context, so you can set this to be anything you want for the call.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind

@ecarriou
Copy link
Member

ecarriou commented Dec 18, 2018

When you want to create private methods / properties in JavaScript, the standard way is to do closure or define all your methods / properties in the constructor.

In System Designer, because we serialize / deserialize methods, we use new Function so we can not have an access to the parent scope. We pass to these methods the instance context, which does not have private information.

A possible solution to have access to private methods / properties in these methods would be to pass an extended context (i.e. instance object with private methods / properties), but that context does not have to interfere with the current instance.

Another solution would be to wait for native implementation of private field in all browsers, and then use it.

@matthewkastor
Copy link

I'm pretty sure you are missing some pretty fundamental concepts about how JavaScript actually works. I've got to just step away from this conversation and leave you to continue your journey. Have fun, and I wish you luck!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants