Maison > Tutoriel CMS > WordPresse > le corps du texte

Comment déterminer s'il s'agit d'une valeur booléenne

WBOY
Libérer: 2023-08-31 10:25:12
original
984 Les gens l'ont consulté

Comment déterminer sil sagit dune valeur booléenne

Boolean() 构造函数可用于创建布尔对象以及布尔原始值,表示 truefalse 值。

在下面的代码中,我详细介绍了 JavaScript 中布尔值的创建。

示例:sample52.html

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

	// Create a Boolean object using the new keyword and the Boolean() constructor.
	var myBoolean1 = new Boolean(false); // Using new keyword.
	console.log(typeof myBoolean1); // Logs 'object'.

	// Create a Boolean literal/primitive by directly using the number constructor without new.
	var myBoolean2 = Boolean(0); // Without new keyword.
	console.log(typeof myBoolean2); // Logs 'boolean'.

	// Create Boolean literal/primitive (constructor leveraged behind the scenes).
	var myBoolean3 = false;
	console.log(typeof myBoolean3); // Logs 'boolean'.
	console.log(myBoolean1, myBoolean2, myBoolean3); // Logs false false false.

</script></body></html>
Copier après la connexion

Boolean() 参数

Boolean() 构造函数将一个参数转换为布尔值(即 truefalse)。任何非 0、-0、nullfalseNaNundefined 或空字符串 ("") 的有效 JavaScript 值都将转换为 true。在以下示例中,我们创建两个布尔对象值:一个 true 和一个 false

示例:sample53.html

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

	// Parameter passed to Boolean() = 0 = false, thus foo = false
	var foo = new Boolean(0)
	console.log(foo);

	// Parameter passed to Boolean() = Math = true, thus bar = true
	var bar = new Boolean(Math)
	console.log(bar);

</script></body></html>
Copier après la connexion

当与 new 关键字一起使用时,来自 Boolean() 构造函数的实例会生成一个实际的复杂对象。您应该避免使用 Boolean() 构造函数创建布尔值(而是使用文字/原始数字),因为存在与 typeof 运算符相关的潜在问题。 typeof 运算符将布尔对象报告为“object”,而不是您可能期望的原始标签(“boolean”)。此外,文字/原始值的写入速度更快。


Boolean() 属性和方法

Boolean() 对象具有以下属性:

属性(例如,Boolean.prototype;):

  • 原型

布尔对象实例属性和方法

布尔对象实例具有以下属性和方法(不包括继承的属性和方法):

实例属性(例如,var myBoolean = false; myBoolean.constructor;):

  • 构造函数

实例方法(例如,var myNumber = false; myBoolean.toString();):

  • toSource()
  • toString()
  • valueOf()

非原始 False 布尔对象转换为 True

Boolean() 构造函数创建的 false 布尔对象(而不是原始值)是一个对象,并且对象会转换为 true。因此,当通过 Boolean() 构造函数创建 false 布尔对象时,该值本身会转换为 true。在下面的示例中,我演示了 false 布尔对象如何始终是“true”。

示例:sample54.html

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

	var falseValue = new Boolean(false);

	console.log(falseValue); // We have a false Boolean object, but objects are truthy.

	if (falseValue) { // Boolean objects, even false Boolean objects, are truthy.
		console.log('falseValue is truthy');
	}

</script></body></html>
Copier après la connexion

如果需要将非布尔值转换为布尔值,只需使用 Boolean() 构造函数,而不使用 new 关键字,返回的值将是原始值而不是布尔对象。


某些事情是假的,其他一切都是真的

已经提到过,但值得再次提及,因为它与转换有关:如果值为 0、-0、nullfalseNaNundefined,或空字符串( ""),就是false。如果在布尔上下文中使用,除上述值之外的 JavaScript 中的任何值都将转换为 true(即 if (true) {};)。

示例:sample55.html

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

	// All of these return a false Boolean value.
	console.log(Boolean(0));
	console.log(Boolean(-0));
	console.log(Boolean(null));
	console.log(Boolean(false));
	console.log(Boolean(''));
	console.log(Boolean(undefined));
	console.log(Boolean(null));

	// All of these return a true Boolean value.
	console.log(Boolean(1789));
	console.log(Boolean('false')); // 'false' as a string is not false the Boolean value.
	console.log(Boolean(Math));
	console.log(Boolean(Array()));

</script></body></html>
Copier après la connexion

结论

了解哪些 JavaScript 值被简化为 false 至关重要,这样您就知道所有其他值都被视为 true

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!