Reputation: 7154
I've noticed in Javascript there are 2 different ways to use functions:
string.trim();
parseInt(string)
What are the differences?
Are the one in point #1 not functions? How are they called?
I know inside parenthesis you can have more variables, but why is str.trim()
and not trim(str)
?
Upvotes: 0
Views: 52
Reputation: 6968
They are both functions, but I believe the first example in your question is normally called a method.
I'd encourage you to read the really useful Functions chapter of Eloquent Javascript. Just a couple of extracts that seem relevant:
A function definition is just a regular variable definition where the value given to the variable happens to be a function.
and
A function is created by an expression that starts with the keyword function. Functions have a set of parameters (in this case, only x) and a body, which contains the statements that are to be executed when the function is called. The function body must always be wrapped in braces, even when it consists of only a single statement (as in the previous example).
A function can have multiple parameters or no parameters at all.
I would also keep in mind that in JS almost everything is an object, or as someone else put it:
Since functions are objects, they can be used like any other value. Functions can be stored in variables, objects, and arrays. Functions can be passed as arguments to functions, and functions can be returned from functions. Also, since functions are objects, functions can have methods
Upvotes: 2