js 經常使用總結

1.截取字符串json

  var a="/s/d";   console.log(a.substr(0,a.indexOf("/",1)))  // 獲得/s數組

2. //json數組 合併去重保留後面的
  var songs = [
    {name:"羽根",artist:"羽根"},
    {name:"晴天",artist:"晴天1"},
    {artist:"周杰倫",name:"周杰倫1"}
  ];

  var arr = [
    {name:"羽根",artist:"羽根2"},
    {name:"晴天",artist:"晴天2"},
  ]
  for (let i = 0; i < arr.length; i++) {
    songs.push(arr[i])
  }
  function unique(songs){
    let result = {};
     let finalResult=[];
    for(let i=0;i<songs.length;i++){
       result[songs[i].name]=songs[i];
    }
    for(let item in result){
       finalResult.push(result[item]);
     }
    return finalResult;
  }
  console.log(JSON.stringify(unique(songs))); //[{"name":"羽根","artist":"羽根2"},{"name":"晴天","artist":"晴天2"},{"artist":"周杰倫","name":"周杰倫1"}]

this

3. 正則 替換<括號spa

  this.detailsForm.map(item => {
     if(item.indexOf("<") != -1) {
       let trunItem = item.replace(/\</g, '&lt');
      this.lists.push(trunItem);
     } else {
       this.lists.push(item);
    }prototype

    })orm

4.  js 判斷數據類型方法及 缺點   (  typeof,instanceof,constructor,Object.prototype.toString.call()    )對象

1.  typeof字符串

  console.log(typeof 2);                        // number
  console.log(typeof true);                    // boolean
  console.log(typeof 'str');                    // string
  console.log(typeof []);                       // object     []數組的數據類型在 typeof 中被解釋爲 object
  console.log(typeof function(){});       // function
  console.log(typeof {});                      // object
  console.log(typeof undefined);        // undefined
  console.log(typeof null);                 // object     null 的數據類型被 typeof 解釋爲 object
原型

   缺點:  沒法判斷 [] {} 和null string

2. instanceof

  console.log(2 instanceof Number);                           // false
  console.log(true instanceof Boolean);                       // false
  console.log('str' instanceof String);                            // false  
  console.log([] instanceof Array);                                // true
  console.log(function(){} instanceof Function);            // true
  console.log({} instanceof Object);                               // true    
  console.log(undefined instanceof Undefined);            //  Undefined is not defined
    console.log(null instanceof Null);                                  //   Null is not defined

  console.log(new Number(2) instanceof Number);        // true
  console.log(new Boolean(true) instanceof Boolean);    // true
  console.log(new String('str') instanceof String);             // true

       缺點:  instanceof 定義:  判斷一個對象是不是數據類型的實例。    2, true ,'str'不是實例,因此判斷值爲false   沒法判斷null和undefined 

3. constructor

  console.log((2).constructor === Number);                // true
  console.log((true).constructor === Boolean);         // true
  console.log(('str').constructor === String);             // true
  console.log(([]).constructor === Array);           // true
  console.log((function() {}).constructor === Function);     // true
  console.log(({}).constructor === Object);           // true

   缺點:  若是原型被更改則類型也被更改 eg:

    function Fn(){};
    Fn.prototype=new Array();
    var f=new Fn();
    console.log(f.constructor===Fn);    // false
    console.log(f.constructor===Array); // true

4. Object.prototype.toString.call()

  var a = Object.prototype.toString;
  console.log(a.call(2));                         "[object Number]"
  console.log(a.call(true));      "[object Boolean]"
  console.log(a.call('str'));          "[object String]"
  console.log(a.call([]));        "[object Object]"
  console.log(a.call(function(){}));   "[object Function]"
  console.log(a.call({}));        "[object Array]"
  console.log(a.call(undefined));     "[object Undefined]"
  console.log(a.call(null));       "[object Null]"

   完美

相關文章
相關標籤/搜索