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

Replace deprecated String.prototype.substr() #23525

Merged
merged 1 commit into from
Feb 18, 2022
Merged

Replace deprecated String.prototype.substr() #23525

merged 1 commit into from
Feb 18, 2022

Conversation

CommanderRoot
Copy link
Contributor

String.prototype.substr() is deprecated so we replace it with String.prototype.slice() which works similarily but isn't deprecated.
.substr() probably isn't going away anytime soon but the change is trivial so it doesn't hurt to do it.

@Mugen87
Copy link
Collaborator

Mugen87 commented Feb 18, 2022

It seems the webgl_loader_svg is breaking with this change.

String.prototype.substr() is deprecated (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr) so we replace it with slice() which works similarily but isn't deprecated.
Signed-off-by: Tobias Speicher <[email protected]>
@CommanderRoot
Copy link
Contributor Author

This had me puzzled for longer than I want to admit. I fixed it now and the CI tests are passing.

@mrdoob
Copy link
Owner

mrdoob commented Feb 18, 2022

Why slice() instead of substring()?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring

@CommanderRoot
Copy link
Contributor Author

Both have their use cases. Both return the same in most cases but can differ if the second parameter is smaller than the first or negative.
I personally like that slice() has less "magic" and is more likely to act the way you want to (or think it does). For example in cases where you expect the second parameter to be possibly negative and you want to replicate substr() you would use substring() other than that I like to use slice()

@mrdoob
Copy link
Owner

mrdoob commented Feb 18, 2022

Seems like substring() is more strict?

@CommanderRoot
Copy link
Contributor Author

I would say the opposite due to the parameter switching substring() can do but I guess it really depends on how you plan to provide the input variables to these functions.

@mrdoob
Copy link
Owner

mrdoob commented Feb 18, 2022

For example in cases where you expect the second parameter to be possibly negative and you want to replicate substr() you would use substring()

Hmm.... Is that why you're using substring() in ColladaLoader.js and webgl_geometry_text.html?

@mrdoob mrdoob added this to the r138 milestone Feb 18, 2022
@CommanderRoot
Copy link
Contributor Author

That's correct. In both cases the first parameter is >= 0. If the second one would be negative substring() will return an empty string (like substr()), slice() however would return everything from the start until the last x chars

@mrdoob mrdoob merged commit fbe603e into mrdoob:dev Feb 18, 2022
@mrdoob
Copy link
Owner

mrdoob commented Feb 18, 2022

Thanks!

donmccurdy pushed a commit to donmccurdy/three.js that referenced this pull request Mar 10, 2022
String.prototype.substr() is deprecated (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr) so we replace it with slice() which works similarily but isn't deprecated.
Signed-off-by: Tobias Speicher <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants