自动扣javascript_JavaScript自动装箱

自动扣javascript

Have you heard of wrapper classes? This is a very common term in many languages. The idea is that primitive, just like classes, can be wrapped into a class object and access the set of methods that are available upon initialization of the objects.

您听说过包装器类吗? 这是许多语言中非常常见的术语。 这个想法是,基元就像类一样可以包装到类对象中,并访问在对象初始化时可用的方法集。

Alright, how does that relate to autoboxing?

好吧,这与自动装箱有何关系?

In fact, the concept of autoboxing works exactly the way wrapper classes do.

实际上,自动装箱的概念与包装器类完全一样。

Let’s talk about primitives because autoboxing is the idea of implicitly turning primitive into object classes when we are to access certain methods that primitives “appear” to have. To understand how autoboxing works, we need to understand how primitives behave.

让我们来谈谈基元,因为自动装箱的想法是当我们访问基元“看起来”具有的某些方法时,将基元隐式地转换为对象类。 要了解自动装箱的工作原理,我们需要了解原语的行为。

Primitives in javascript(or to be honest, any other language as well) are data that has no objects and no methods. Here are the following primitives in javascript:

javascript中的原始数据(或者说其他任何一种语言,也是如此)是没有对象也没有方法的数据。 以下是javascript中的以下原语:

  • string

  • number

  • bigint

    比金特
  • boolean

    布尔值
  • undefined

    未定义
  • symbol

    符号

Because primitives aren’t objects and do not have their own methods, we should be able to assume we cannot call a method of these primitives right?

因为基元不是对象并且没有自己的方法,所以我们应该能够假设我们不能调用这些基元的方法对吗?

Hmm…theoretically yes but that’s not the case in the following example:

嗯...从理论上讲是的,但是在以下示例中并非如此:

Wait what? How come we can just call a method from a primitive? If we actually ran this piece of code on the developer console or via node, not only does it not thrown an error but it actually gives us proper inputs.

等等什么 我们怎么能从原始中调用方法呢? 如果我们实际上在开发人员控制台或通过节点上运行了这段代码,那么它不仅不会引发错误,而且实际上会为我们提供适当的输入。

That doesn’t make sense right? Didn’t we say primitive don’t have access to methods?

那没有道理吧? 我们不是说原始人无法访问方法吗?

Well…here is what happens.

好吧...这是发生了什么。

When we call a method of a primitive, the primitive itself is automatically wrapped into a wrapper object. That’s right! You just heard it…it turns into an object with a set of prototype methods.

当我们调用基元的方法时,基元本身会自动包装到包装对象中。 那就对了! 您刚刚听到了……它将变成具有一组原型方法的对象。

For example, when we declare const number = 1 and we try to access number.valueOf(), number gets autoboxed into a Number object and then it access one of its prototype methods, valueOf, in which it returns 1.

例如,当我们声明const number = 1并尝试访问number.valueOf()时,number被自动装箱到Number对象中,然后它访问其原型方法之一valueOf,并在其中返回1。

Image for post

If you console.log Number._proto_, you will be able to see more methods available to the Number object.

如果您使用console.log Number._proto_,则可以看到Number对象可用的更多方法。

Likewise, when we call a method on a string primitive, the same thing happens. It gets autoboxed into a string object, which is attached with a lot of useful prototypical methods such as substring and concat. These methods are some of the most commonly used methods out there.

同样,当我们在字符串基元上调用方法时,也会发生相同的事情。 它被自动装箱到一个字符串对象中,该字符串对象附带有许多有用的原型方法,例如substring和concat。 这些方法是其中一些最常用的方法。

If you are curious about other methods that come with primitives, you should read the documentation about javascript prototypes online. Array and string prototype methods are especially useful and common in the javascript world.

如果您对原语附带的其他方法感到好奇,则应在线阅读有关javascript原型的文档。 数组和字符串原型方法在javascript世界中特别有用且常见。

That’s it! You learned how autoboxing works in javascript.

而已! 您了解了自动装箱在javascript中的工作方式。

翻译自: https://medium.com/weekly-webtips/autoboxing-in-javascript-a368b42d8969

自动扣javascript

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值