1、JS数据类型
JS数据类型 | |
---|---|
6种值类型(基本类型) | 符串(String)、数字(Number)、布尔(Boolean)、对空(Null)、未定义(Undefined)、Symbol(独一无二的值) |
3种引用数据类型(对象类型) | 对象(Object)、数组(Array)、函数(Function) |
2种特殊的对象 | 正则(RegExp)和日期(Date) |
<script>
console.log(typeof "John") // string
console.log(typeof 3.14) // number
console.log(typeof NaN) // number
console.log(typeof false) // boolean
console.log(typeof [1, 2, 3, 4]) // object
console.log(typeof {
name: 'John', age: 34 }) // object
console.log(typeof new Date()) // object
console.log(typeof function () {
}) // function
console.log(typeof myCar) // undefined
console.log(typeof null) // object
console.log(typeof undefined) // undefined
</script>
2、检测数据类型(四种方法)
- 1、请补全JavaScript函数,要求以字符串的形式返回参数的类型
function _typeof(value) {
// 补全代码
return typeof(value);
}
- 2、请补全JavaScript函数,要求以Boolean的形式返回第一个参数是否属于第二个参数对象的实例。
function _instanceof(left,right) {
// 补全代码
return (left instanceof right);
}
总结:
1、js检测数据类型四种办法
方法 | 使用 |
---|---|
typeof | 1、typeof null 返回类型错误,返回object 。2、引用类型,除了function返回function类型外,其他均返回object。3、 引用类型中的 数组、日期、正则 也都有属于自己的具体类型,而 typeof 对于这些类型的处理,只返回了处于其原型链最顶端的 Object 类型 |
instanceof | instanceof 只能用来判断两个对象是否属于实例关系, 而不能判断一个对象实例具体属于哪种类型 |
constructor | constructor是原型prototype的一个属性,当函数被定义时候,js引擎会为函数添加原型prototype,并且这个prototype中constructor属性指向函数引用, 因此重写prototype会丢失原来的constructor |
Object.prototype.toString.call() | 对于 Object 对象,直接调用 toString() 就能返回 [object Object] 。而对于其他对象,则需要通过 call / apply 来调用才能返回正确的类型信息 |
来源:https://www.cnblogs.com/yadiblogs/p/10750775.html
2、Object.prototype.toString.call() 的使用(推荐)
<script>
/* Object.prototype.toString.call */
console.log(Object.prototype.toString.call('')) // [object String]
console.log(Object.prototype.toString.call(1)) // [object Number]
console.log(Object.prototype.toString.call(true)) // [object Boolean]
console.log(Object.prototype.toString.call(Symbol)) // [object Function]
console.log(Object.prototype.toString.call(undefined)) // [object Undefined]
console.log(Object.prototype.toString.call(null)) // [object Null]
console.log(Object.prototype.toString.call(new Function())) // [object Function]
console.log(Object.prototype.toString.call(new Date())) // [object Date]
console.log(Object.prototype.toString.call([])) // [object Array]
console.log(Object.prototype.toString.call(new RegExp())) // [object RegExp]
console.log(Object.prototype.toString.call(new Error())) // [object Error]
console.log(Object.prototype.toString.call(document)) // [object HTMLDocument]
console.log(Object.prototype.toString.call(window)) // [object global] window 是全局对象 global 的引用
</script>
3、数据类型转换(toString)
- 请补全JavaScript函数,要求以字符串的形式返回两个数字参数的拼接结果。
function _splice(left,right) {
// 补全代码
return left.toString()+right.toString();
}
总结:
数据类型转换 | |
---|---|
String()、toString() | 转换为字符串类型 |
Number() | 转换为数字类型 |
Boolean() | 转换为布尔类型 |
具体请看:https://www.html.cn/qa/javascript/11339.html
4、阶乘(递归)
- 请补全JavaScript函数,要求返回数字参数的阶乘。注意:参数为大于等于0的整数
function _factorial(number) {
// 补全代码
if(number==1){
return number;
}else{
return number * _factorial(number - 1)
}
}
5、绝对值
- 请补全JavaScript函数,要求返回数字参数的绝对值
function _abs(number) {
// 补全代码
return number>=0?number:-number