Methods of primitives

JavaScript Methods of Primitives (图元的JavaScript方法)

In JavaScript, it is possible to work with primitives (numbers, strings, and more), as if they were objects. (在JavaScript中,可以使用基元(数字、字符串等) ,就好像它们是对象一样。)

But, of course, there are notable differences between objects and primitives. (但是,当然,对象和原语之间存在显着差异。)

So, primitive is a primitive type value. Seven types of primitives exist, among them are: number, bigint, symbol, string, boolean, null and undefined. An object can store multiple values as properties. You can create an object using {}. For example:

{
 name: "Chevrolet",
 model: "Camaro",
 price: 40000
}

Other kinds of objects also exist in JavaScript. For example, functions are also considered as objects. (JavaScript中还存在其他类型的对象。例如,函数也被视为对象。)

One of the most significant advantages of objects is that function can be stored as one of its properties. (对象最重要的优点之一是函数可以存储为其属性之一。)

For instance:

let site = {
 name: "w3cdoc",
 welcome: function () {
   console.log("Welcome to w3cdoc!");
 }
};
site.welcome(); // Welcome to w3cdoc!

There are many built-in objects, as well. For example, those that work with errors, dates, HTML elements, and more. They include different methods and properties. (还有许多内置对象。例如,适用于错误、日期、HTML元素等的元素。它们包括不同的方法和属性。)

But, note that objects need additional resources for supporting the internal machinery. (但是,请注意,对象需要额外的资源来支持内部机器。)

A Primitive as an Object

A Primitive as an Object (基元作为对象)

Below is described the paradox of JavaScript:

  • One would wish to do many things with primitives such as strings and numbers. Accessing them as methods would be a great thing. (-人们希望用字符串和数字等原语做很多事情。将它们作为方法访问将是一件好事。)

  • Primitives have to be both lightweight and fast. (-基元必须既轻巧又快速。)

And here is the solution, which may seem a little awkward:

  • Primitives remain primitives. One value, as aspired. (-原语仍然是原语。一个值,如愿以偿。)

  • In the language, you have access to the properties and methods of strings, symbols, booleans, and numbers. (-在语言中,您可以访问字符串、符号、布尔值和数字的属性和方法。)

  • For forcing that work, a unique “object wrapper,” which provides extra functionality is generated, and then is demolished. (-为了强制执行该工作,将生成一个独特的“对象包装器” ,该包装器提供额外的功能,然后将其拆除。)

Each type of a primitive has its own “object wrapper” called: Number, Boolean, String, Symbol. They provide various sets of methods. For example, there is a string method str.toUpperCase(), returning a capitalized str.

It works in this way:

let str = "Welcome to w3cdoc";
console.log(str.toUpperCase()); // WELCOME TO w3cdoc

Now, let’s see what happens in str.toUpperCase():

  • str is a primitive. A unique object is created in the moment of accessing its property. That object knows the string value and has helpful methods, such as toUpperCase(). (- str是一个原语。在访问其属性时创建一个唯一对象。该对象知道字符串值并具有有用的方法,例如toUpperCase ()。)

  • That method is capable of running and returning an entirely new string ( it is shown by console.log). (-该方法能够运行并返回一个全新的字符串(由console.log显示)。)

  • The special object is demolished, and the primitive str is left alone. (-特殊对象被拆除,原始字符串被单独留下。)

We can assume that primitives provide methods, but remain lightweight at the same time. (我们可以假设基元提供方法,但同时保持轻量级。)

It is important to note that JavaScript greatly enhances this process. Moreover, the creation of an extra object can be skipped. Anyway, it should still be adhere to the specification behaving as if it generates one. (需要注意的是, JavaScript大大增强了这一过程。此外,可以跳过创建额外对象。无论如何,它仍然应该遵守规范,就好像它生成了一个规范一样。)

A number has its methods. Let’s have a look at this example:

let num = 1.23456;
console.log(num.toFixed(3)); // 1.235

Here, toFixed(n) rounds the number to a particular precision. (在这里, toFixed (n)将数字舍入到特定的精度。)

For instance:

let num = 1.23456;
console.log(num.toFixed(2)); // 1.23

Note that you can use the constructors String/Number/Boolean only internally. (请注意,只能在内部使用构造函数String/Number/Boolean。)

Like some other languages, like Java, give opportunities to create “wrapper objects” for primitives with the following syntax:

new Number(1);
or
new Boolean(false);

Theoretically, it is possible in JavaScript too, but we don’t recommend you to do so. It might bring unwanted outcomes. For example:

console.log(typeof 0); // "number"
console.log(typeof new Number(0)); // "object"

In if, objects are always truthy. Here the alert will show the following:

let zero = new Number(0);
if (zero) { // zero is true, because it's an object
 console.log("In this case, zero is truthy!");
}

Another useful thing is using the same functions String/Number/Booleanwithout new. With the help of them, value is converted to a string, a number and a boolean. (另一个有用的事情是使用相同的函数String/Number/Boolean而不是new。在他们的帮助下,值被转换为字符串、数字和布尔值。)

Here is an example:

let num = Number("12"); // convert a string to number
console.log(typeof num);

Summary

Summary (概要)

Primitive has a range of helpful methods (except null and undefined). Those methods operate via temporary objects. The engines of JavaScript are properly tuned to enhance that internally. Therefore, it is not expensive to call. (Primitive有一系列有用的方法( null和undefined除外)。 这些方法通过临时对象进行操作。 JavaScript的引擎经过适当调整,可以在内部进行增强。 因此,打电话并不昂贵。)



请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部