gpt4 book ai didi

javascript - 为什么 Javascript 具有某些功能而另一些功能又如此?

转载 作者:行者123 更新时间:2023-12-02 15:49:09 25 4
gpt4 key购买 nike

我注意到在 Javascript 中有两种不同的方法来使用函数:

  1. 使用变量后跟一个点,然后是函数,例如 string.trim();
  2. 在函数内部使用变量,例如parseInt(string)

有什么区别?

第一点中的那个不是函数吗?他们怎么称呼?

我知道括号内可以有更多变量,但为什么是 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 - 为什么 Javascript 具有某些功能而另一些功能又如此?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31985558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com