-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
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
Evaluate ES6 classes #11552
Comments
So you're suggesting using this pattern instead? PointLight.prototype = Object.create( Light.prototype );
Object.assign( PointLight.prototype, { |
class PointLight extends Light hehe 😄 And no problems... |
@sasha240100 someday... |
@mrdoob not quite - the two ways you mention are directly equivalent. I think the OP is comparing PointLight.prototype = Object.assign( Object.create( Light.prototype ), {
constructor: PointLight,
prop1: 'something',
method1: function someFunction() { .. },
...
}); with function PointLight () { ... };
PointLight.prototype = Object.create( Light.prototype );
PointLight.prototype.constructor = PointLight;
PointLight.prototype.prop1 = 'something';
PointLight.prototype.method1 = function someFunction() { .. };
... Which is the way it's done here for example. |
@looeee @bfred-it @mrdoob Why not just using Comparison: import { LineBasicMaterial } from './LineBasicMaterial';
import { Color } from '../math/Color';
function LineDashedMaterial( parameters ) {
LineBasicMaterial.call( this );
this.type = 'LineDashedMaterial';
this.scale = 1;
this.dashSize = 3;
this.gapSize = 1;
this.setValues( parameters );
}
LineDashedMaterial.prototype = Object.create( LineBasicMaterial.prototype );
LineDashedMaterial.prototype.constructor = LineDashedMaterial;
LineDashedMaterial.prototype.isLineDashedMaterial = true;
LineDashedMaterial.prototype.copy = function ( source ) {
LineBasicMaterial.prototype.copy.call( this, source );
this.scale = source.scale;
this.dashSize = source.dashSize;
this.gapSize = source.gapSize;
return this;
};
export { LineDashedMaterial }; ES2015+. Same code, but es2015+ with import { LineBasicMaterial } from './LineBasicMaterial';
import { Color } from '../math/Color';
export class LineDashedMaterial extends LineBasicMaterial {
type = 'LineDashedMaterial';
scale = 1;
dashSize = 3;
gapSize = 1;
isLineDashedMaterial = true;
constructor(parameters) {
super();
this.setValues( parameters );
}
copy(source) {
super.copy(source);
this.scale = source.scale;
this.dashSize = source.dashSize;
this.gapSize = source.gapSize;
return this;
}
} ES6 features that would simplify three.js code: |
I'm all for moving to ES2015+, we just need to find a way to output similar code than what we currently have out of it, so the performance stays the same in all cases. |
I have a question in context of classes. How would we transfer methods like Vector3.unproject into the class syntax? The method actually uses a closure in order to create a new scope. This is an important mechanism that keeps the amount of object creations as low as possible. Do we need |
@Mugen87 @mrdoob Some interesting info on es6 performance. Especially on |
@Mugen87 Can't they just be non-exported module scoped objects? Something like this;
|
Ah yes, i think this should work 😊 |
@mrdoob Wow! It seems already working. Can't we make a branch, transform some classes to es6 and see how it compiles? @satori99 As an idea of how to keep export default class Vector3 {
static tempMatrix = new Matrix();
unproject() {
// uses Vector3.tempMatrix
}
} |
Sound good to me! I'm currently focusing on WebVR so it'll need to be someone else than me. |
@sasha240100 The benefit of using module scoped vars is that they remain hidden from regular user code, which seems appropriate for temp variables. I don't mind the "We are all adults here" pythonistic approach in regards to private vars, but temp variables shouldn't really pollute the namespace unnecessarily. Also, It would be nice if those of us who have enabled native module support in our browsers could load the src files directly. This is a much more pleasant way to develop, without needing watchers and transpiling after every edit. Using class properties means this isn't possible as they are not part of the current class spec. |
Apologies for butting in. We should probably change the issue title to something like - "Evaluate ES6 classes", since now the thread has changed to something completely different. |
Why not try typescript, it can be compiled into other versions of js |
TypeScript really would be a great option to think about because it's a transpiler + type checker and a superset of JavaScript, so it's easy to move a code base over to .ts files and gradually refactor to ES6 with type checking. It may sound scary if you've never used TypeScript, but it's really not a big learning curve and would be a small price to pay for the benefits it would bring. The TypeScript community would be very happy to help with this transition and creating performance testing against the current library to make sure it's not being downgraded. A few useful articles:
|
Until browsers can execute natively TypeScript, I prefer to keep using JavaScript. |
I can't see that as a valid reason for not using TypeScript solely on the reason it can't be run directly in the browser. You wouldn't want it to run in the browser because of all the extra lines of code that is intended only for compile time checking. It's not currently a runtime checking language. So if it was ever used in the browser it's more than likely going to have all the typed code stripped away because it impacts performance and this would then be vanilla JavaScript code. I think you're totally missing the point of using a typed language and the benefits it has in development in a large code base. You are still writing and using JavaScript, the whole point of TypeScript is that it is a superset of JavaScript. You write JavaScript with types, that's compiled into JavaScript in the specified ECMAScript target version, which is configurable in the compiler options, the permitted values are 'es3', 'es5', 'es2015', 'es2016', 'es2017' or 'esnext'. Because Typescript is JavaScript it makes it possible to progressively migrate without having a massive headache of refactoring everything at once. It can be gradually done and improved by the community. It's no more work than what's being discussed here with refactoring to use ES6 classes. That's the only reason I mention it here instead of opening a new issue. See TypeScript playground links below for great examples: |
Can you think of anyone that could possibly disagree with you about typescript being the best solution to this particular problem? If you type in No types seems like more of a compromise than choosing one of these. |
Save Typescript for projects that are more complicated than the result they create -- specially frontend frameworks that could have been implemented in HTML in the first place. My original point was to get rid of the JavaScript disease, not make it worse. JavaScript is a simple almost toy language that sometimes is used for complex results like three.js. Typescript is pointless.
… On Sep 6, 2017, at 1:55 PM, Joe ***@***.***> wrote:
@mrdoob
I can't see that as a valid reason for not using TypeScript solely on the reason it can't be run directly in the browser. You wouldn't want it to run in the browser because of all the extra lines of code that is intended only for compile time checking. It's not currently a runtime checking language. So if it was ever used in the browser it's more than likely going to have all the typed code stripped away because it impacts performance and this would then be vanilla JavaScript code.
I think you're totally missing the point of using a typed language and the benefits it has in development in a large code base. You are still writing and using JavaScript, the whole point of TypeScript is that it is a superset of JavaScript. You write JavaScript with types, that's compiled into JavaScript in the specified ECMAScript target version, which is configurable in the compiler options, the permitted values are 'es3', 'es5', 'es2015', 'es2016', 'es2017' or 'esnext'.
Because Typescript is JavaScript it makes it possible to progressively migrate without having a massive headache of refactoring everything at once. It can be gradually done and improved by the community. It's no more work than what's being discussed here with refactoring to use ES6 classes. That's the only reason I mention it here instead of opening a new issue.
See TypeScript playground links for examples:
Classic JavaScript Example
Adding Types Example
Adding Types with error Example
Using Classes Example
Using Classes with error Example
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
^ i'd be fine even with the monstrous pattern, as long as it's consistent. |
@joejordanbrown sounds like you're in love with typescript. feel free to fork the project and port it to typescript. three.ts! 🙌 |
That's a matter of choice, I'm sure many will agree and disagree, that's normal though right! You're always going to see "this vs that", "mine is better than yours". I understand that each has their own benefits. It's about weighing up the options available and seeing if they can benefit the project. Comparisons are a good thing, it pushes projects further. You mention Flow, the problems I see with that are:
Even google is backing TypeScript in a big way, that shows the confidence they have in TypeScript. Read the post here or here.
I just thought I'd open the discussion about using a Typed language and see what everyone else thinks about the idea. It does seem that @mrdoob is totally against even discussing the idea though. @arctwelve @mrdoob |
Not to play the devils advocate, but it seems like he did
it was just really short :) I share a similar standpoint, it's a JS library and JS is standardized. You can't go wrong with choosing JS for a JS library, while you can if you choose something else. I just took Flow as one of the alternatives to typescript, dunno if there are others. Anyway, it seems like we really went off topic.
The original title referred to (as far as i understand) to the lack of style consistency. In particular using I imagine that with both typescript and es6, the code should be fairly consistent. I'd address this issue by updating this page: https://github.com/mrdoob/three.js/wiki/Mr.doob's-Code-Style%E2%84%A2 and adding either: A) "...use Object.assign ..." |
It's in the first post. I suggest:
|
Indeed. @joejordanbrown feel free to create a new topic to discuss TypeScript. Btw, it's also bad OSS practice to ignore previous conversations... #341 (comment) |
Reposted from closed issue. I wanted to create this issue to try and tease out everyone's thoughts on how we'd like to move forward with class migration. My quick summary of what I've come across so far:
Have I missed anything? |
I'm not sure who is responsible for any specific next step on the |
further to that, it appears that we're just waiting on a date to be set |
Also, as part of a bit of revision I did, I found this comment about how other ES2015 features could be introduced via the current rollup build process. There is even an example given. edit: here's a gist of a another quick example |
@DefinitelyMaybe I've got some time and I'd love to help. Can I take some items on the |
@DefinitelyMaybe What is the best way to deal with inheritance from a deep ancestor like
|
we were going to make a note at the top of the file about any issues we run into, if Im remembering correctly. |
I misunderstood what was happening with AudioListener... After some debugging, I think there is a matching issue in the |
this will be the case for some of the files but not all. We were anticipating such an issue. |
For those following along, please have a quick read of the discussion over here. In the mean time, dibs on |
Closing in favor of #19986. |
Why is this 'idiom' of inheritance being introduced into the code:
PointLight.prototype = Object.assign( Object.create( Light.prototype ), {
Seriously? Function(nested Function(ParentPrototype) COMMA SHOTGUN BRACKET?
The faithful two line style still in, for example, the Materials classes are much clearer and cleaner. Assign the prototype and then set the constructor. The end. Please don't ruin the library by catching JavaScript disease - the bizarre need to masturbate the way objects and inheritance are coded. One style throughout the library. No need to change it.
The text was updated successfully, but these errors were encountered: