JS advanced — function is used as return value

Posted on

Function is used as the return value

 

function f1() {
      console.log ("F1 function start");
      return function () {
        console.log ("function is used as return value");
      }
    }

 

Get the data type of the variable num
Determine whether the object is of a certain type
    var num = 10;
    console.log(typeof num); //num

    var obj = {};
    console.log(obj instanceof Object); //true

 

//In this case, the data type of the object [object object] is output
    console.log(Object.prototype.toString());
    //The data type of the output array [object array]
    console.log(Object.prototype.toString.call([]));

 

    var arr = [10, 20, 30];
    console.log(Object.prototype.toString.call(arr)); //[object Array]

    var arr = [10, 20, 30];
    console.log(Object.prototype.toString.call(arr)); // [object Array]
    console.log(Object.prototype.toString.call(new Date()));//[object Date]

 

//Determine whether the object and the type passed in are of the same type
function getFunc(type) {
      return function (obj) {
        return Object.prototype.toString.call(obj) === type;
      }
    }

 

    var ff = getFunc("[object Array]");
    var result = ff([10, 20, 30]);
    console.log(result); //true

    var ff1 = getFunc("[object Object]");
    var dt = new Date();
    var result1 = ff1(dt);
    console.log(result1); //false

 

Leave a Reply

Your email address will not be published.