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

Can't use ES6 class as View #194

Closed
PaulMaly opened this issue Dec 16, 2017 · 2 comments
Closed

Can't use ES6 class as View #194

PaulMaly opened this issue Dec 16, 2017 · 2 comments
Labels

Comments

@PaulMaly
Copy link

Some strange restriction I found. I can't just use ES6 class as DomVM view even if class has render() method. Could you please provide more info about this problem? Thanks!

@leeoniya
Copy link
Member

leeoniya commented Dec 17, 2017

hey @PaulMaly,

this topic and discussion will probably need to be turned into a wiki or added to the docs

here's the situation.

ES6 classes are technically not difficult to support* in domvm. they actually were supported at one point.

however, domvm intentionally avoids requiring the use of this because it's ambiguous in many contexts outside of OOP. there are many articles and experts that have weighed in on the subject over the years, [1] among them. a reliance on this makes it impossible to make functions pure. to avoid requiring this, domvm passes vm as the first argument into many functions. this is also set to be equivalent to vm, but its use is strongly discouraged for the stated reasons.

* if ES6 classes are supported, the calling convention of all functions would have to change based on whether the the view is a class or not, so that vm is no longer passed as the first argument. this dynamic switching of calling conventions significantly affects performance. alternatively, we can leave the arguments as-is, but it would be rather unconventional to also have vm passed in as arg 0 to all instance methods:

class MyView {
  diff(vm, data) {
    vm === this;
  }

  render(vm, data) {
    vm === this;
  }
}

rather than

class MyView {
  diff(data) {
    this;
  }

  render(data) {
    this;
  }
}

The thing is, everything you can do with ES6 classes, you can do better with composition using Object.assign(), Object.create() and ES6 modules. domvm supports plain object views [2] that give you the freedom to extend and compose. If you'd like specific examples of how to avoid ES6 classes in cases you need, I would be happy to provide some.

[1] http://justjs.com/posts/this-considered-harmful
[2] https://github.com/leeoniya/domvm#views

@leeoniya
Copy link
Member

i've added this to the docs: https://github.com/leeoniya/domvm#es6es2015-classes

while i don't believe there's a compelling reason for adding support for ES6 class views, i'm open to being convinced otherwise. i'm going to close this issue but feel free to continue the discussion here. i'll reopen it if there's a use-case than cannot be cleanly handled by the current architecture without classes. fwiw, React parity is not one of this project's goals.

if you'd like something like domvm but more react-like, take a look at https://github.com/thysultan/dio.js

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

No branches or pull requests

2 participants