-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Records and methods #384
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
Comments
I do this for adding methods to record instances: var Person = Immutable.Record({ name: '' });
Person.prototype.sayHi = function() {
return this.name;
}); |
For now, you can also get an efficient structure and methods by defining your own objects with function Complex(p)
{
this.real = p.real;
this.image = p.image;
}
Complex.prototype.equals = function(other)
{
return this.real === other.real && this.image === other.image;
};
Complex.prototype.hashCode = function()
{
return util.hash_int32array([
this.real,
this.image,
]);
};
Complex.prototype.magnitude = function()
{
return Math.sqrt(this.real*this.real + this.image*this.image);
}; |
@copy, I'm glad you've caught onto this pattern! I haven't documented it anywhere yet, but in the next major version, I will document this as a way to define "value" objects that work in the Immutable.js ecosystem (eg, can be used as keys, and are considered values by Immutable.is()) |
Methods can be added to Record classes as @kentor describes. Simply add methods to the prototype. It is probably not a good idea to add them as values in the Record. Some ES6 transpilers support extending expressions, in which case you can write: class Complex extends Record({real:1, image:1}) { For the transpilers that don't support subclassing an expression, but support an identifier, you can write: var _Complex = Record({...}) I personally prefer just adding methods to the prototype. |
Can we return altered versions of records from Record methods? I have the following:
as a method of a Record class, and when I call it, I get back something that the debugger claims is an instance of MyRecord, but there is no |
I found that declaring methods as properties like that, it works OK within the same file. But the methods do not appear when accessed outside this file. I had luck rewriting as actual methods:
|
It seems, Records are of great interest and it is one of killer features of Immutable.js. I start to use it, but unfortunately ES6 is not yet there for extending Records with methods and ES5 inheritance is pain.
For now, I "embed" methods into Records like this:
In reality, there is for example isNull method that shows the record has no real value. It would be useful to allow prototype extending in Record constructor, not sure it is acceptable for functional programming purists though.
I also heavily support the idea of making Records efficient (#286). Records can be lightweight structures, they are often used for small "value" types and they never change keys
The text was updated successfully, but these errors were encountered: