ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 语言基础 - 数据类型 ``` <pre class="calibre14">``` undefined<span class="token2">,</span><span class="token5">null</span><span class="token2">,</span>boolean<span class="token2">,</span>number<span class="token2">,</span>string<span class="token2">,</span>Array基本数据类型 Object复杂数据类型 ``` ``` - 类型转换 ``` <pre class="calibre14">``` <span class="token1">parseInt</span><span class="token2">(</span><span class="token4">"666"</span> <span class="token2">)</span> 非数字返回NaN <span class="token1">parseFloat</span><span class="token2">(</span><span class="token4">"2.45"</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">(</span><span class="token3">1</span><span class="token2">)</span><span class="token2">.</span><span class="token1">toString</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">(</span><span class="token3">10</span><span class="token2">)</span><span class="token2">.</span><span class="token1">toString</span><span class="token2">(</span><span class="token3">2</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//二进制</span> <span class="token1">String</span><span class="token2">(</span>undefined<span class="token2">)</span><span class="token2">;</span> <span class="token5">null</span>也会输出字符串 <span class="token">!</span><span class="token">!</span><span class="token4">"3.5"</span> <span class="token3">true</span> typeof <span class="token5">null</span><span class="token2">;</span> object ``` ``` - 字符串 ``` <pre class="calibre14">``` <span class="token4">"123我爱你"</span><span class="token2">.</span><span class="token1">charAt</span><span class="token2">(</span><span class="token3">3</span><span class="token2">)</span> 返回我<span class="token2">,</span><span class="token3">3</span>是位置 <span class="token4">"123我爱你"</span><span class="token2">.</span><span class="token1">indexOf</span><span class="token2">(</span><span class="token4">"我"</span><span class="token2">)</span> 返回<span class="token3">3</span> 找不到<span class="token">-</span><span class="token3">1</span> <span class="token4">"123我爱你"</span><span class="token2">.</span><span class="token1">match</span><span class="token2">(</span><span class="token">/</span>爱<span class="token2">(</span><span class="token2">.</span><span class="token">*</span><span class="token2">)</span><span class="token">/</span><span class="token2">)</span> 返回数组 <span class="token2">[</span><span class="token4">"爱你"</span><span class="token2">,</span> <span class="token4">"你"</span><span class="token2">]</span> 没有找到返回 <span class="token5">null</span> <span class="token4">"我爱你中国美国英国"</span><span class="token2">.</span><span class="token1">substring</span><span class="token2">(</span><span class="token3">1</span><span class="token2">,</span><span class="token3">4</span><span class="token2">)</span><span class="token2">;</span> 返回爱你中<span class="token2">,</span>从第<span class="token3">1</span>到<span class="token3">4</span>不包括<span class="token3">4</span> <span class="token4">"我爱你中国美国英国"</span><span class="token2">.</span><span class="token1">substr</span><span class="token2">(</span><span class="token3">3</span><span class="token2">,</span><span class="token3">5</span><span class="token2">)</span> 第三个开始截取<span class="token3">5</span>个字符 <span class="token4">"我爱你中国人"</span><span class="token2">.</span><span class="token1">replace</span><span class="token2">(</span><span class="token">/</span>中国<span class="token">/</span><span class="token2">,</span><span class="token4">"美国"</span><span class="token2">)</span><span class="token2">;</span> <span class="token4">"我爱你中国人"</span><span class="token2">.</span><span class="token1">search</span><span class="token2">(</span><span class="token">/</span>中国<span class="token">/</span><span class="token2">)</span><span class="token2">;</span> 找到返回下标 没找到返回<span class="token">-</span><span class="token3">1</span> <span class="token4">"123我爱你"</span><span class="token2">.</span><span class="token1">split</span><span class="token2">(</span><span class="token4">""</span><span class="token2">)</span><span class="token2">;</span> 返回数组 支持正则 <span class="token4">"aA"</span><span class="token2">.</span><span class="token1">toLowerCase</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span>转小写 toUpperCase大写 var string <span class="token">=</span> <span class="token2">(</span><span class="token3">2</span><span class="token2">)</span><span class="token2">.</span><span class="token1">toString</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> var string <span class="token">=</span> <span class="token3">2</span><span class="token">+</span><span class="token4">''</span><span class="token2">;</span> ``` ``` - 数组 ``` <pre class="calibre14">``` var arr <span class="token">=</span><span class="token1">Array</span><span class="token2">(</span><span class="token3">1</span><span class="token2">,</span><span class="token3">2</span><span class="token2">,</span><span class="token3">3</span><span class="token2">)</span><span class="token2">;</span> var arr <span class="token">=</span><span class="token2">[</span><span class="token3">1</span><span class="token2">,</span><span class="token3">2</span><span class="token2">,</span><span class="token3">3</span><span class="token2">]</span><span class="token2">;</span> var arr <span class="token">=</span><span class="token2">{</span><span class="token4">"31536000"</span><span class="token2">:</span><span class="token4">"年"</span><span class="token2">,</span><span class="token4">"2592000"</span><span class="token2">:</span><span class="token4">"个月"</span><span class="token2">,</span><span class="token4">"604800"</span><span class="token2">:</span><span class="token4">"星期"</span><span class="token2">,</span><span class="token4">"86400"</span><span class="token2">:</span><span class="token4">"天"</span><span class="token2">,</span><span class="token4">"3600"</span><span class="token2">:</span><span class="token4">"小时"</span><span class="token2">,</span><span class="token4">"60"</span><span class="token2">:</span><span class="token4">"分钟"</span><span class="token2">,</span><span class="token4">"1"</span><span class="token2">:</span><span class="token4">"秒"</span><span class="token2">}</span><span class="token2">;</span> <span class="token2">[</span><span class="token3">1</span><span class="token2">,</span><span class="token3">2</span><span class="token2">,</span><span class="token3">3</span><span class="token2">]</span><span class="token2">.</span><span class="token1">join</span><span class="token2">(</span><span class="token4">','</span><span class="token2">)</span><span class="token6">//数组转换字符串</span> <span class="token4">'1,2,3'</span><span class="token2">.</span><span class="token1">split</span><span class="token2">(</span><span class="token4">','</span><span class="token2">)</span><span class="token6">//字符串转换数组</span> $<span class="token2">.</span><span class="token1">isArray</span><span class="token2">(</span><span class="token2">[</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span><span class="token6">// 是否数组 返回true</span> arr<span class="token2">.</span><span class="token1">push</span><span class="token2">(</span><span class="token4">"white"</span><span class="token2">,</span><span class="token4">"test"</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//尾部插入数组值 返回长度</span> arr<span class="token2">.</span><span class="token1">unshift</span><span class="token2">(</span><span class="token4">"first"</span><span class="token2">)</span><span class="token2">;</span> <span class="token6">//头部插入</span> arr<span class="token2">.</span><span class="token1">pop</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//尾部删除 返回被删除元素</span> arr<span class="token2">.</span><span class="token1">shift</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//头部删除</span> arr<span class="token2">.</span><span class="token1">reverse</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//颠倒数组</span> arr<span class="token2">.</span><span class="token1">sort</span><span class="token2">(</span><span class="token5">function</span><span class="token2">(</span>num1<span class="token2">,</span> num2<span class="token2">)</span> <span class="token2">{</span> <span class="token5">return</span> num1 <span class="token">></span> num2<span class="token2">;</span><span class="token2">}</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//排序 返回排序后</span> arr<span class="token2">.</span><span class="token1">concat</span><span class="token2">(</span><span class="token2">[</span><span class="token4">"ccc"</span><span class="token2">,</span><span class="token4">"bbbb"</span><span class="token2">]</span><span class="token2">,</span><span class="token4">'3333'</span><span class="token2">,</span><span class="token2">[</span><span class="token4">'vvccxx'</span><span class="token2">,</span><span class="token2">[</span><span class="token4">'oolll'</span><span class="token2">,</span><span class="token4">'lll'</span><span class="token2">]</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//合并数组 返回新数据</span> arr<span class="token2">.</span><span class="token1">slice</span><span class="token2">(</span><span class="token3">1</span><span class="token2">,</span><span class="token3">3</span><span class="token2">)</span><span class="token2">;</span> <span class="token6">//复制数组 从第一个开始第三个结束 返回新数组</span> arr<span class="token2">.</span><span class="token1">splice</span><span class="token2">(</span><span class="token3">0</span><span class="token2">,</span><span class="token3">2</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//删除前两个</span> arr<span class="token2">.</span><span class="token1">splice</span><span class="token2">(</span><span class="token3">1</span><span class="token2">,</span><span class="token3">1</span><span class="token2">,</span><span class="token3">100</span><span class="token2">,</span><span class="token3">200</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//从第二个删除1个 并添加2个新元素</span> var max <span class="token">=</span> Math<span class="token2">.</span>max<span class="token2">.</span><span class="token1">apply</span><span class="token2">(</span>Math<span class="token2">,</span> arr<span class="token2">)</span> <span class="token6">//3</span> var min <span class="token">=</span> Math<span class="token2">.</span>min<span class="token2">.</span><span class="token1">apply</span><span class="token2">(</span>Math<span class="token2">,</span>arr<span class="token2">)</span> <span class="token6">//1</span> arr<span class="token2">.</span><span class="token1">indexOf</span><span class="token2">(</span>str<span class="token2">)</span><span class="token2">;</span><span class="token6">//查找元素位置 字符操作</span> var arr<span class="token">=</span><span class="token2">[</span><span class="token4">"中国"</span><span class="token2">,</span><span class="token4">"美国"</span><span class="token2">,</span><span class="token4">"日本"</span><span class="token2">,</span><span class="token4">"韩国"</span><span class="token2">]</span><span class="token2">;</span> arr<span class="token2">.</span><span class="token1">forEach</span><span class="token2">(</span><span class="token5">function</span><span class="token2">(</span>v<span class="token2">,</span>k<span class="token2">)</span><span class="token2">{</span> <span class="token1">log</span><span class="token2">(</span>k<span class="token2">)</span>无返回值 <span class="token2">}</span><span class="token2">)</span> arr<span class="token2">.</span><span class="token1">map</span><span class="token2">(</span><span class="token5">function</span><span class="token2">(</span>value<span class="token2">,</span>index<span class="token2">,</span>array<span class="token2">)</span><span class="token2">{</span>   <span class="token5">return</span> XXX <span class="token2">}</span><span class="token2">)</span> ``` ``` - json与对象 ``` <pre class="calibre14">``` var str <span class="token">=</span> <span class="token4">'{"a":"2a","b":true,"c":34,"d":[1,2,"a"]}'</span><span class="token2">;</span><span class="token6">//json操作</span> JSON<span class="token2">.</span><span class="token1">parse</span><span class="token2">(</span>str<span class="token2">)</span><span class="token2">;</span><span class="token6">//json字符串转换成对象</span> var obj <span class="token">=</span> <span class="token1">eval</span><span class="token2">(</span><span class="token4">'('</span> <span class="token">+</span> str <span class="token">+</span> <span class="token4">')'</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//3种方法</span> var k1 <span class="token">=</span> <span class="token2">{</span>a<span class="token2">:</span><span class="token3">1</span><span class="token2">,</span>b<span class="token2">:</span><span class="token2">[</span><span class="token3">1</span><span class="token2">,</span><span class="token3">2</span><span class="token2">]</span><span class="token2">}</span><span class="token2">;</span><span class="token6">//对象</span> var k2 <span class="token">=</span> <span class="token2">{</span><span class="token4">"k1"</span><span class="token2">:</span><span class="token3">1</span><span class="token2">,</span><span class="token4">"k2"</span><span class="token2">:</span><span class="token4">"中国"</span><span class="token2">}</span><span class="token2">;</span><span class="token6">//json标准格式 或者是关联数组</span> JSON<span class="token2">.</span><span class="token1">stringify</span><span class="token2">(</span>k1<span class="token2">)</span> <span class="token6">//json对象转换成字符串</span> ``` ``` - 时间戳13位长 ``` <pre class="calibre16">``` <span class="token">+</span><span class="token5">new</span> <span class="token1">Date</span><span class="token2">(</span><span class="token2">)</span> 当前时间戳 <span class="token">+</span><span class="token5">new</span> <span class="token1">Date</span><span class="token2">(</span><span class="token4">'2014/12/24 02:08:01'</span><span class="token2">)</span><span class="token2">;</span> 转换时间戳 Date<span class="token2">.</span><span class="token1">now</span><span class="token2">(</span><span class="token2">)</span> 当前时间戳 <span class="token5">new</span> <span class="token1">Date</span><span class="token2">(</span>Date<span class="token2">.</span><span class="token1">parse</span><span class="token2">(</span><span class="token4">"2012-12-15 09:41:30"</span><span class="token2">.</span><span class="token1">replace</span><span class="token2">(</span><span class="token">/</span><span class="token">-</span><span class="token">/</span>g<span class="token2">,</span><span class="token4">"/"</span><span class="token2">)</span><span class="token2">)</span><span class="token2">)</span><span class="token2">;</span> Date对象 <span class="token5">new</span> <span class="token1">Date</span><span class="token2">(</span><span class="token3">1419184274000</span><span class="token2">)</span> Date对象 ``` ```