JavaScript 中如何检查值是否为原始值?

在本教程中,我们将了解检查给定数据类型是否为的方法 是否原始。
JavaScript 中的数据类型 1. 原始数据类型 2. 非原始数据类型
原始数据类型 - 字符串、数字、未定义、布尔、null、符号、bigint。
非原始数据类型 - 对象
原始数据类型/值不是对象,它表示为 语言实现的底层。所有原始值都是不可变的 这意味着您不能更改它们的类型,而是可以将 v 新值重新分配给 变量。
为了检查该值是否是原始值,我们检查给定的值是否是一个对象;如果 我们提供的值是一个对象,这意味着它不是使用某些的原始数据类型 方法。
方法一:使用Object()
我们将使用严格相等运算符检查给定值是否为对象类型 因为它还会检查数据类型和值。它首先将值转换为 对象,因为我们将通过对象传递值作为参数。如果我们的价值是 一个对象,那么对象函数将返回相同的对象,并且它将被视为一个 对象,否则严格相等运算符将检查并返回 false,因为类型不会 是一样的。
语法
inputValue !== Object(inputValue);
让我们定义一个函数来检查输入值是否为原始类型。
function isPrimitive(inputValue){
if(inputValue===Object(inputValue)){
return "Value is not primitive";
}
else{
return "Value is primitive";
}
}
示例
In the example below, we check the following values if they are 是否原始。
空
数字
字符串
字符串对象
布尔值
数组
空数组
对象文字
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>Check if the value is primitive or not</h2>
<p id="result"></p>
</body>
<script type="text/javascript">
function isPrimitive(inputValue) {
if (inputValue === Object(inputValue)) {
console.log("Value is not primitive")
document.getElementById("result").innerHTML += inputValue +": not primitive <br>"
} else {
console.log("Value is primitive")
document.getElementById("result").innerHTML += inputValue +": primitive <br>"
}
}
isPrimitive(null)
isPrimitive(12)
isPrimitive("This is simple string")
isPrimitive(new String("This is string object"))
isPrimitive(false)
isPrimitive([1, 2, 3])
isPrimitive([])
isPrimitive({})
</script>
</html>
方法2:使用typeof运算符
在此方法中,我们将使用 typeof 运算符检查数据类型,并且我们知道非原始数据类型始终是对象类型,因此我们将检查我们的值是否是以下类型 反对与否。
如果我们的值不是对象或函数的类型,那么它就是原始值;否则它就不是原始的。 我们还必须处理 null 的情况,因为 null 是原始类型值,但 typeof 会 如果我们检查 typeof(null),则将输出显示为对象。
function isPrimitive(inputValue){
if(inputValue==null)
{
return "Value is primitive";
}
if(typeof(inputValue)=="function" || typeof(inputValue)=="object"){
return "Value is not primitive"
}
else{
return "Value is not primitive"
}
}
示例
在下面的示例中,我们检查不同的值是否是原始值。检查是否 值是否是原始值,我们使用了 typeof 运算符。我们检查类型是否是 函数或对象。如果类型是函数或对象,则该值不是基元 类型;否则它是原始的。
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>Check if the value is primitive or not</h2>
<div id="result"></div>
</body>
<script type="text/javascript">
function isPrimitive(inputValue){
if(inputValue==null)
{
return `primitive <br>`;
}
if(typeof(inputValue)=="function" || typeof(inputValue)=="object"){
return `not primitive <br>`;
}
else{
return `primitive <br>`;
}
}
let resultDiv = document.getElementById("result");
resultDiv.innerHTML += "12: " + isPrimitive(12);
resultDiv.innerHTML += "null: " + isPrimitive(null);
resultDiv.innerHTML += "false: " + isPrimitive(false);
resultDiv.innerHTML += "[1,2,3]: " + isPrimitive([1,2,3]);
resultDiv.innerHTML += `"This is simple string": ` + isPrimitive("This is simple string");
resultDiv.innerHTML += "new String(): " + isPrimitive(new String("This is string object"));
resultDiv.innerHTML += "[]: " + isPrimitive([]);
resultDiv.innerHTML += "{}: " + isPrimitive({});
resultDiv.innerHTML += "new Date(): " + isPrimitive(new Date());
</script>
</html>
因此,我们需要了解检查给定值是原始类型值还是非原始值的方法。
javascript