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?
数字值,例如 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.
发布评论
评论(1)
一些评论者将您带到资源中,并提供有关JavaScript中原型的更多信息,并且有关该主题的更多信息此stackoverflow问题。
但是,让我们尝试对手头的问题提供一个简单的答案。
应该注意的是,虽然原型方法非常常用,但您很少明确键入
number.protype
之类的内容。在JavaScript上,每个对象(包括代表原始值(如数字)的对象)都具有原型对象其继承属性和方法。原型几乎总是一个实际的对象,尽管它也可能是
null
。数字值,例如
0
或235.5
,将号码
对象作为其原型。现在,这可能有点令人困惑,但是
number.prototype
没有涉及number
对象的原型。相反,这是number
对象用作原型对象时提供的。因此,例如,如果您编写(23).ToLocalestring()
1 ,那么您是在调用okbocttolocalestring > 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
or235.5
, have theNumber
object as their prototype.Now, it may be a bit confusing, but
Number.prototype
does not refer to the prototype of theNumber
object. Instead, it is what theNumber
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 methodtoLocaleString
of the object23
, which inherits it from theNumber
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 write23..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.