Home > CMS Tutorial > WordPress > body text

Use basic strings, numbers, and Boolean values

PHPz
Release: 2023-08-29 18:25:02
Original
1052 people have browsed it

Use basic strings, numbers, and Boolean values

Don't be confused by the fact that strings, numbers, and Boolean literals can be considered objects with properties (for example, true.toString()). When these primitive values ​​are treated as objects by trying to access their properties, JavaScript will create a wrapper object from the primitive's associated constructor so that the properties and methods of the wrapper object can be accessed.

Once the property is accessed, the wrapper object is discarded. This conversion allows us to write code that looks as if the original value is actually an object. To be honest, when it's treated as an object in code, JavaScript converts it to an object so that property access can work, and then converts it back to the original value after returning the value. The key thing to note here is what's going on, and JavaScript is doing it for you behind the scenes.


Original value sample

Here are some examples to demonstrate what I'm talking about:

String example: sample56.html

<!DOCTYPE html><html lang="en"><body><script>

	// String object treated like an object.
	var stringObject = new String('foo');
	console.log(stringObject.length); // Logs 3.
	console.log(stringObject['length']); // Logs 3.

	// String literal/primitive converted to an object when treated as an object.
	var stringLiteral = 'foo';
	console.log(stringLiteral.length); // Logs 3.
	console.log(stringLiteral['length']); // Logs 3.
	console.log('bar'.length); // Logs 3.
	console.log('bar'['length']); // Logs 3.

</script></body></html>
Copy after login

Number example: sample57.html

<!DOCTYPE html><html lang="en"><body><script>

	// Number object treated like an object.
	var numberObject = new Number(1.10023);
	console.log(numberObject.toFixed()); // Logs 1.
	console.log(numberObject['toFixed']()); // Logs 1.

	// Number literal/primitive converted to an object when treated as an object.
	var numberLiteral = 1.10023;
	console.log(numberLiteral.toFixed()); // Logs 1.
	console.log(numberLiteral['toFixed']()); // Logs 1.
	console.log((1234).toString()); // Logs '1234'.
	console.log(1234['toString']()); // Logs '1234'.

</script></body></html>
Copy after login

Boolean example: sample58.html

<!DOCTYPE html><html lang="en"><body><script>

	// Boolean object treated like an object.
	var booleanObject = new Boolean(0);
	console.log(booleanObject.toString()); // Logs 'false'.
	console.log(booleanObject['toString']()); // Logs 'false'.

	// Boolean literal/primitive converted to an object when treated as an object.
	var booleanLiteral = false;
	console.log(booleanLiteral.toString()); // Logs 'false'.
	console.log(booleanLiteral['toString']()); // Logs 'false'.
	console.log((true).toString()); // Logs 'true'.
	console.log(true['toString']()); // Logs 'true'.

</script></body></html>
Copy after login

When accessing the properties of a raw number directly (not stored in a variable), the number must be calculated before the value can be treated as an object (for example, (1).toString(); or 1..toString();). Why two points? The first dot is treated as a numeric decimal, not an operator used to access the object's properties.


You should generally use raw strings, numbers, and booleans

Literal/raw values ​​representing strings, numbers, or Boolean values ​​are written faster and in a more concise literal form.

Therefore, you should use literal values. Additionally, the accuracy of the typeof operator depends on how you create the value (literal vs. constructor call). If you create a string, number, or Boolean object, the typeof operator reports the type as an object. If you use literals, the typeof operator returns the string name of the actual value type (for example, typeof 'foo' // returns 'string').

I demonstrate this fact in the code below.

Example: sample59.html

<!DOCTYPE html><html lang="en"><body><script>

	// String, number, and Boolean objects.
	console.log(typeof new String('foo')); // Logs 'object'.
	console.log(typeof new Number(1)); // Logs 'object'.
	console.log(typeof new Boolean(true)); // Logs 'object'.

	// String, number, and Boolean literals/primitives.
	console.log(typeof 'foo'); // Logs 'string'.
	console.log(typeof 1); // Logs 'number'.
	console.log(typeof true); // Logs 'boolean'.

</script></body></html>
Copy after login

in conclusion

If your program relies on the typeof operator to identify strings, numbers, or Boolean values ​​based on these basic types, you should avoid using String, Number and Boolean constructor.

The above is the detailed content of Use basic strings, numbers, and Boolean values. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!