为什么 Javascript 在某种程度上有一些功能,而在另一种方面有一些功能?
Why Javascript has some functions in a way and some in another?
我注意到 Javascript 有两种不同的函数使用方式:
- 使用变量后跟一个点,然后是函数,如
string.trim();
- 在函数内部使用变量,如
parseInt(string)
有什么区别?
第 1 点中的那个没有功能吗?他们怎么称呼?
我知道括号内可以有更多变量,但为什么是 str.trim()
而不是 trim(str)
?
它们都是函数,但我相信您问题中的第一个示例通常称为方法。
我鼓励您阅读真正有用的内容 Functions chapter of Eloquent Javascript。只是一些看似相关的摘录:
A function definition is just a regular variable definition where the
value given to the variable happens to be a function.
和
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.
我还要记住,在 JS 中几乎所有东西都是对象,或者如 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
我注意到 Javascript 有两种不同的函数使用方式:
- 使用变量后跟一个点,然后是函数,如
string.trim();
- 在函数内部使用变量,如
parseInt(string)
有什么区别?
第 1 点中的那个没有功能吗?他们怎么称呼?
我知道括号内可以有更多变量,但为什么是 str.trim()
而不是 trim(str)
?
它们都是函数,但我相信您问题中的第一个示例通常称为方法。
我鼓励您阅读真正有用的内容 Functions chapter of Eloquent Javascript。只是一些看似相关的摘录:
A function definition is just a regular variable definition where the value given to the variable happens to be a function.
和
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.
我还要记住,在 JS 中几乎所有东西都是对象,或者如 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