什么是“原型”在number.prototype.tofixed()中?

发布于 2025-02-06 09:29:53 字数 415 浏览 1 评论 0 原文

我正在尝试学习JavaScript。为了使用开发人员Mozilla ,有必要编写.Prototype。原型是什么意思,为什么要使用它?您能为编程语言刚接触的人提供适当的解释吗?

I am trying to learn JavaScript. In order to use some methods of the Number object on the Developer Mozilla, it is necessary to write a .prototype. What does prototype here mean and why should it be used? Can you give a proper explanation for someone new to the programing language?

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

临走之时 2025-02-13 09:29:53

一些评论者将您带到资源中,并提供有关JavaScript中原型的更多信息,并且有关该主题的更多信息此stackoverflow问题

但是,让我们尝试对手头的问题提供一个简单的答案。

应该注意的是,虽然原型方法非常常用,但您很少明确键入 number.protype 之类的内容。

在JavaScript上,每个对象(包括代表原始值(如数字)的对象)都具有原型对象其继承属性和方法。原型几乎总是一个实际的对象,尽管它也可能是 null

数字值,例如 0 235.5 ,将号码对象作为其原型。
现在,这可能有点令人困惑,但是 number.prototype 没有涉及 number 对象的原型。相反,这是 number 对象用作原型对象时提供的。因此,例如,如果您编写(23).ToLocalestring() 1 ,那么您是在调用okboct tolocalestring > 23 ,它从号码对象继承。

1:我们必须围绕 23 添加括号的原因是因为将被解释为小数点,导致语法错误。另一个选项是编写 23..tolecalestring()其中第一个点将是一个尾随的小数点( 23。是在JavaScript中写23的有效方法)和 second dot将用于属性访问。

Some commenters directed you to resources with more information about prototypes in JavaScript, and there is also more information about the topic on the answers for this StackOverflow question.

However, let's try to give a simple answer to the question at hand.

It should be noted that while the prototype methods are very common to use, you rarely explicitly type something like Number.prototype.

On JavaScript, each object (including objects that represent primitive values like numbers) has a prototype object from which it inherits properties and methods. The prototype is almost always an actual object though it may also be Null.

Number values, like 0 or 235.5, have the Number object as their prototype.
Now, it may be a bit confusing, but Number.prototype does not refer to the prototype of the Number object. Instead, it is what the Number object provides when used as a prototype, to it's instance objects. So if, for example, you write (23).toLocaleString()1, then you are calling the method toLocaleString of the object 23, which inherits it from the Number object.

1: The reason we have to add parentheses around 23 is because the . would otherwise be interpreted as decimal point, resulting in a syntax error. Another option would be to write 23..toLocaleString() where the first dot would be a trailing decimal point (23. is a valid way to write 23 in JavaScript) and the second dot would be used for the property access.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文