-
-
Notifications
You must be signed in to change notification settings - Fork 407
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
Implement Array.prototype.forEach #192
Conversation
var sum = 0; | ||
var indexSum = 0; | ||
var listLengthSum = 0; | ||
function callingCallback(item, index, list) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could test using this
inside the callback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, come to think of it, I am not sure this
has been implemented. However, you could add a TODO for when it is.
let this_arg = args | ||
.get(1) | ||
.cloned() | ||
.unwrap_or_else(|| Gc::new(ValueData::Undefined)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can replace this line with .unwrap_or(&Gc::new(ValueData::Undefined));
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clippy
is going to complain that you are not using unwrap_or_else()
.
let this_arg = args | ||
.get(1) | ||
.cloned() | ||
.unwrap_or_else(|| Gc::new(ValueData::Undefined)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use the undefined()
function, defined in src/lib/builtins/value.rs
let element = this.get_field(&i.to_string()); | ||
let arguments = vec![element.clone(), to_value(i), this.clone()]; | ||
|
||
interpreter.call(callback_arg, &this_arg, arguments)?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
callback
should not be called for undefined values. Also, can you please add tests for cases where array is modified in the callback? For ex. new elements are added to array, elements inserted in the middle, elements deleted.
MDN has a few examples on how this function should work in such cases here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Im not sure this is quite right, it does run for undefined values, just not values that have been deleted
@xSke do you need any help on this? |
Replace by jasonwilliams#268 |
No description provided.