Skip to content

Incorrect information of Data Types/Array Methods about thisArg #3766

Open
@anastasiosPou

Description

@anastasiosPou

I was reading the article on Data Types, Array Methods when I saw the example code for thisArg. The example code below:
Example code

The explanation why the code would give a problem if we omit the thisArg argument here is:

If in the example above we used users.filter(army.canJoin), then army.canJoin would be called as a standalone function, with this=undefined, thus leading to an instant error.

The canJoin isn't called as a standalone function. It's been called in the context of army with the dot syntax. Inside canJoin where we use the 'this' keyword, 'this' points correctly to army. The problem here for some reason is the (implicit in that case as we haven't define it) is the user member of the users array. That's what causes the error and not the omission of the thisArg.

I believe that a whole section about how 'this' works would be highly valuable as it's extremely important to know and often a source of bugs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions