How to create fields in a class using Symbol - javascript

I have class Movie. Movie constructor should provide a generation of unique product id within the application no matter how many products are created. You also need to define a field with the name of the movie. But according to the condition, for this I have to use Symbol data type. How can i do this?
class Movie {
constructor(name) {
//here I need to generate a unique id;
//here I need to define name fields
}
}

class Movie {
sym = Symbol('symbol description')
constructor(name) {
this.symInConstructor = Symbol('symbol description')
// this.sym != this.symInConstructor
// M1.sym == M2.sym <=> M1 == M2
}
}
// remeber, Symbol() != Symbol(), each call creates a unique one
However note that you can't ever serialize a Symbol.
If you need a serializable thingy, generate uuid or whatever
Also it may have sense to not use Symbol, but to use the class instance itself, I don't see how the Symbol may be used

Create a global constant, and use square brackets access
const MovieName = Symbol('a key for MovieName')
class Movie {
[MovieName] = "The Movie";
["myString"] = "is equivalent to:"
// myString = "is equivalent to:"
// and must be used for Symbols as
constructor() {
this[MovieName] = "The Movie in constructor"
}
// may keep is somewhere like this for usage in other places
static movieNameSymbol = MovieName
}

Related

Class with grouped properties in deeper levels

I'm trying to build a complex class where I want to group properties, making the instantiated object have multiple layers, instead of every property being at root level.
So far, the only way I've found to do this is by making a class with the properties to group, and then in a "parent" class add a property of the class I built.
The problem here though is that two properties not sharing the same class can't communicate with each other.
There are ways around this, but I find them all very hacky and looking bad. One would be to create a hidden element, and store data in there that a property from another class can read.
Another would be to create static properties, but then, unless you do some major work with that property, you can only have one object created from the parent class, as it'll be the same no matter the instantiation of the class.
Very basic example:
class A {
constructor(prop1){
this.property = prop1;
}
}
class B {
constructor(prop2){
this.property = prop2;
}
}
class C {
constructor(prop1, prop2){
this.PropertyA = new A(prop1);
this.PropertyB = new B(prop2);
}
}
let obj = new C(1, 1);
console.log(obj.PropertyA.property);
In this example, the property from class A can't get a value from property in class B.
So, my question is, is there another way of building the class C to keep the levels of hierarchy in the object?
I use the class structure because I like how it looks. It looks far more readable to me than the prototype structure, and I'm not building an object directly, as I would like to instantiate more of them.
It feels like I have forgotten things I've looked at to try to do this, but I'm sure it'll come to me soon enough after I post this.
Sooo...
I worked a bit on a static-solution, and basically made a private static property to hold a unique id per instantiated object, with the key-value pairs I want to be able to share between the different classes. This should only expose the methods to either set or get those values. The only requirement is that all the classes needs to be constructed with the object ID, so they can get the right value.
I understand that people will roll their eyes at my infantile tries to break the actual points of classes and such, but it works for me anyway in this specific circumstance anyway.
I'm sure there a multitude of ways to update it to ensure it runs more smoothly, but I think it works for most cases at the moment.
The code made in example code:
"use strict";
class A {
#id
#testProp
constructor(id){
this.#id = id;
this.#testProp = 10;
}
get TestProp(){ return this.#testProp + C.getSharedProp(this.#id, "BValue")};
set TestProp(newValue) { this.#testProp = newValue; C.setSharedProp(this.#id, "AValue", this.#testProp) };
}
class B {
#id
#testProp
constructor(id){
this.#id = id;
this.#testProp = 10;
}
get TestProp(){ return this.#testProp + C.getSharedProp(this.#id, "AValue")};
set TestProp(newValue) { this.#testProp = newValue; C.setSharedProp(this.#id, "BValue", this.#testProp) };
}
class C {
#id
constructor(){
this.#id = Math.random().toString(36).substr(2, 9);
this.PropertyA = new A(this.#id);
this.PropertyB = new B(this.#id);
}
static #sharedProps = {};
static getSharedProp(charId, valueName) {
if(!charId){
throw "Must supply character ID";
}
if(!valueName){
throw "Must supply name of value to return";
}
if(!(charId in this.#sharedProps)){
throw "Character ID not found";
}
if(!(valueName in this.#sharedProps[charId])){
throw valueName + "-element not found";
}
return this.#sharedProps[charId][valueName];
}
static setSharedProp(charId, valueName, value) {
if(!charId){
throw "Must supply character ID";
}
if(!valueName){
throw "Must supply name of value";
}
if(!(charId in this.#sharedProps)){
this.#sharedProps[charId] = [];
}
if(!(valueName in this.#sharedProps[charId])){
this.#sharedProps[charId][valueName] = -1;
}
if(!value){
console.warn("Value not supplied of " + valueName + ". Not updating extant value");
}else{
this.#sharedProps[charId][valueName] = value;
}
}
}
let obj = new C();
obj.PropertyA.TestProp = 20;
obj.PropertyB.TestProp = 5;
console.log(obj.PropertyA.TestProp); //should be 25; 20 from its own class and 5 from foreign class-object
console.log(obj.PropertyB.TestProp); //should be 25; 5 from its own class and 20 from foreign class-object

How can I stop JavaScript from passing a class by reference?

I have the following code in the constructor of a class (shortened for the purpose of the question):
constructor(effect: EffectInstance, names: string[], count?: number) {
this.effect = effect; // instance of a class "Effect"
let name; for (name of names) {
this.custom.set(name, this.effect); // custom: Map
}
}
EffectInstance is the type of this class, which is generic
When I change this.effect.name in a method of the class, or when I grab the effect from the Map this.custom and change its name, both are changed.
From what I can tell, this is due to JavaScript's pass-by-reference behavior with objects, as I'm 100% certain that I'm not modifying the values I don't want modified. (I'd like to be able to rename the Effect instance in the custom Map, but keep this.effect.name unchanged)
I tried to re-instantiate the classes with the parameters in constructor(), but this raises a new issue: I'd be losing types, and I can't seem to figure out how to work around this. Here's what I tried:
(EffectInstance, for reference: <EffectInstance extends Effect>)
constructor(effect: EffectInstance, names: string[], count?: number) {
this.effect = effect;
let altEffect = effect instanceof PlayerEffect ? new PlayerEffect(effect.name, effect.ignoreRaces) : new Effect(effect.name);
let name; for (name of names) {
this.custom.set(name, altEffect);
}
}
Doing so, TS raises this error on altEffect:
TS2345: Argument of type 'Effect' is not assignable to parameter of type 'EffectInstance'.   'Effect' is assignable to the constraint of type 'EffectInstance', but 'EffectInstance' could be instantiated with a different subtype of constraint 'Effect'.
I need to either stop the pass-by-reference behavior or preserve the type that EffectInstance contains. How can I do this?
Most languages pass objects by reference, so this is not a unique behavior with javascript.
If you want to pass a copy of the class instance and not the original instance and send the clone where you don't want send the original instance.
class Car {
constructor(name) {
this.name = name;
}
}
let orignalClass = new Car('BMW');
let cloneClass = Object.assign(Object.create(Object.getPrototypeOf(orignalClass)), orignalClass)
console.log(orignalClass);
console.log(cloneClass);
// now both can be updated individually
orignalClass.name = 'BMw-1';
cloneClass.name = "BMW-copy"
console.log("after update");
console.log(orignalClass);
console.log(cloneClass);
through your code, this.custom type should be Effect.
constructor(effect: EffectInstance, names: string[], count?: number) {
this.custom = new Map<string, Effect>(); // altEffect type could be PlayerEffect or Effect
this.effect = effect;
let altEffect = effect instanceof PlayerEffect ? new PlayerEffect(effect.name, effect.ignoreRaces) : new Effect(effect.name);
let name; for (name of names) {
this.custom.set(name, altEffect);
}
}

Make long path codes shorter in java

I am pretty new to programming so I am not sure what this is called but in Javascript, for example
arr[0].obj[0].getSomething();
can be shorten to
var o = arr[0].obj[0];
o.getSomething();
so that we do not have to repeat
arr[0].obj[0]
what is the equivalent of this in Java? I can't seem to find it.
Also tell me what should the title be, I am not sure whether my title is appropriate.
You do it just the same way:
If you have
House[] houses; // An array of houses
// initialize and fill the array
... and inside the House class you have a field doors:
public class House {
Door[] doors;
// Initialize the array in a constructor, add getter and setter methods
}
Then you can do either
Color doorColor = houses[0].doors[0].getColor();
or you store the door you want in a variable and then ask for its color:
Door door = houses[0].doors[0];
Color doorColor = door.getColor();
With the given snipped I can understand your java code would look like as below.
class A{
B obj[];
}
class B {
public void doSomething(){}
}
lets say you have array of A class object as as your code snippet says
arr[0].obj[0].getSomething();
in java it would be of class A like below
A arr[]
so the code would be
arr[0].obj[0].doSomething();
we can write it as
A firstA = arr[0];
B firstB = firstA.obj[0];
firstB.doSomething();
or
B firstB = arr[0].obj[0];
firstB.doSomething();

How to serialize an object and cast it back to the same class as the original object

I'm new to JavaScript so bear with me if what I'm asking is not "how you do it in JavaScript". Advice on other approaches are welcome.
I have a class named State and I need need to serialize objects of that class using JSON.stringify(). The next step is to deserialize them back into an objects. However, my class uses setters and getters.
The problem that I'm facing is that after I deserialized those objects the setters and getters seem to be gone. I just cannot figure out how I can properly turn serialized objects back into objects of that class so that they behave exactly the same as objects that are created using new directly.
In another language I would cast those objects into State objects. I cannot find a JavaScript mechanism that seems to work that way.
The code looks as follows:
class State {
constructor(href) {
this.url = href;
}
set url(href) {
this._url = new URL(href);
this.demoParam = this._url.searchParams.get("demoParam");
}
get url() {
return this._url;
}
set demoParam(value) {
let param = parseInt(value, 10);
if(isNaN(param)) {
param = 2;
}
console.log("Setting 'demoParam' to value " + param);
this._demoParam = param;
}
get demoParam() {
return this._demoParam;
}
toJSON() {
let stateObject = {};
const prototypes = Object.getPrototypeOf(this);
for(const key of Object.getOwnPropertyNames(prototypes)) {
const descriptor = Object.getOwnPropertyDescriptor(prototypes, key);
if(descriptor && typeof descriptor.get === 'function') {
stateObject[key] = this[key];
}
}
return stateObject;
}
}
let originalState = new State(window.location.href);
let newState1 = JSON.parse(JSON.stringify(originalState));
newState1.demoParam = 12;
let newState2 = Object.create(State.prototype, Object.getOwnPropertyDescriptors(JSON.parse(JSON.stringify(originalState))));
newState2.demoParam = 13;
let newState3 = Object.assign(new State(window.location.href), JSON.parse(JSON.stringify(originalState)));
newState3.demoParam = 14;
let newState4 = Object.setPrototypeOf(JSON.parse(JSON.stringify(originalState)), State.prototype);
newState4.demoParam = 15;
I would expect that everytime I set the demoParam property of a newStateX object I'd see a console log message. However. I only see it twice, i.e. for every new State(window.location.href) statement.
I have used the answer of this question. However, it does not work as expected.
when you serialize an object you trigger the toString or the toJSON method of your class' instance and end up with just a "dumb" JSON representation of your enumerable attributes.
if you want to recreate an instance that behaves like it did prior to serialisation, you will need to set an extra key/value pair in your toJSON function like ___internalType: 'state' and then later use eg. a switch statement to recreate your specific class with the new MyClass(serializedData)and passing in your serialised instance. Within the constructor of your class, you set all the attributes you need and voilà, you have your "old" instance again
/edit: to clarify the reason why your console logs aren't showing up is because you are not recreating an instance of your class but just creating a new plain Object.
You can use Object.assign to copy plain object data into an "empty" new instance of the class along the lines of this code:
function cast(o) {
if (!o._cls) return o;
var _cls = eval(o._cls);
return Object.assign(new _cls(), o);
}
In JavaScript i personally like to avoid using classes for my data objects. TypeScript offers some better opportunities to solve this problem, one of these is TypedJSON:
https://github.com/JohnWeisz/TypedJSON

Typescript: Create interface based on a class

I got a class called MyClass. It has a bunch of properties.
I want to create an interface that contains the options of that class. Many options have the same name and typing than MyClass, but not all.
Edit: Most options properties are optional.
The goal is to duplicate the least possible.
Right now, I'm using a dummy object to avoid code duplication. Is there a cleaner solution than this?
class MyClass {
callback:(p0:number,p1:string,p2:string[]) => number[];
myAttr:number;
composed:string;
constructor(options:MyClassOptions){
if(options.callback !== undefined)
this.callback = options.callback;
if(options.myAttr !== undefined)
this.myAttr = options.myAttr;
if(options.composedP1 !== undefined && options.composedP2 !== undefined)
this.composed = options.composedP1 + options.composedP2;
}
}
var dummy = <MyClass>null;
interface MyClassOptions {
callback?:typeof dummy.callback;
myAttr?:typeof dummy.myAttr;
composedP1:string;
composedP2:string;
}
Many options have the same name and typing than MyClass, but not all.
If this is the case, there's not a good option.
If you want to have exactly the same members, and your class doesn't have any private or protected properties/methods, you can write
interface MyClassOptions extends MyClass {
extraProp: string;
}
One thing you could do is make a base class with just the options you want to share, make the interface from there with the "real" class you use being a derivative of the base class.

Categories