Basic front end (B): four methods seven kinds of data types and data type of the detected JavaScript

JavaScript data type and type detection

  1. A total of seven data types of the six simple data types, a complex data types.

Simple data types: String, Number, Boolean, Null , Undefined, Symbol.
Complex data types: Object is the only complex data types. These types Array Function reference values can be attributed to the final complex data type Object.

  1. Special basic types: Basic type of packaging

To aid js primitive values, ECMAscript provided three special reference types: Boolean, Number, and String. Whenever reading a primitive type value, the background will create a basic package type of an object corresponding to, so that we can call some methods to manipulate the data.

var s1 = "some text";
// 在这里创建了一个字符串保存在了变量s1,字符串当然是基本类型值。
// 但是此刻我们又调用了s1的方法,基本类型值不是对象,理论上不应该有方法。
//其实,为了让我们实现这种直观的操作,后台已经帮助我们完成了一系列的操作。当我们在第二行代码中访问 s1 变量时,访问过程处于读取模式,而在读取模式中访问字符串时,后台都会自动完成下列处理。
var s2 = s1.substring(2);
/*
1. 创建 String 类型的一个实例;
2. 在实例上调用指定的方法;
3. 销毁这个实例。
var s1 = new String("some text");
var s2 = s1.substring(2);
s1 = null;
*/ 给大家推荐一个前端学习进阶内推交流圈子685910553
  1. Data type detection methods

typeof: For basic types a null (return obejct) outside, can be returned correctly. For complex types except function (return function) outside, will be returned object.

instanceof: instanceof operator to test whether there is an object constructor prototype property in its prototype chain. (I.e. only used to determine whether two objects belonging to the instance relation, but can not determine what type of a specific object instance belongs to).

 var str = 'text';
 str instanceof String;  // false (包装类型)
 var arr = [1, 2, 3];
 arr instanceof Array;   // true
 arr instanceof Object;  // true
 // 会返回 true ,是因为 Object 构造函数的 prototype 属性存在与 arr 这个数组实例的原型链上。

Constructor object with an object type is determined (detected null or undefined when the constructor property type, js being given! Simply null and undefined because no constructor property)

function cstor(variable) {
 if (variable === null || variable === undefined) {
     return 'Null or Undefined';
 }
 
 var cst = variable.constructor;
 
 switch (cst) {
     case Number:
         return 'Number'
     case String:
         return 'String'
     case Boolean:
         return 'Boolean'
     case Array:
         return 'Array'
     case Object:
         return 'Object'
 }
 }   给大家推荐一个前端学习进阶内推交流圈子685910553

4.Object.prototype.toString.call (): Gets [[Class]] internal property of all objects that are owned, returns the string "[object class]".

/**
 * @param type 字符串,要检测的类型的字符串
 * @return 类型检测函数
 * 根据传入的数据类型,返回该类型的类型检测函数
 * 类型检测使用 toString 函数
 */
function isType(type) {
  return function (val) {
    if (Object.prototype.toString.call(val) === `[object ${type}]`) {
      return true;
    }
    return false;
  };
} 给大家推荐一个前端学习进阶内推交流圈子685910553

export let isString = isType('String');
export let isArray = isType('Array');
export let isFunction = isType('Function');
export let isObject = isType('Object');
export let isNumber = isType('Number');

Guess you like

Origin blog.csdn.net/tianduantoutiao/article/details/92430387