1 Star 0 Fork 0

Kaelinda/kaelindaTop

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
15137621729153.html 12.34 KB
一键复制 编辑 原始数据 按行查看 历史
zuosong 提交于 2019-03-12 15:13 . 2019-03-12 15:13:57
<!doctype html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>
ES6笔记(解构赋值) - kaelinda
</title>
<link href="atom.xml" rel="alternate" title="kaelinda" type="application/atom+xml">
<link rel="stylesheet" href="asset/css/foundation.min.css" />
<link rel="stylesheet" href="asset/css/docs.css" />
<script src="asset/js/vendor/modernizr.js"></script>
<script src="asset/js/vendor/jquery.js"></script>
<script src="asset/highlightjs/highlight.pack.js"></script>
<link href="asset/highlightjs/styles/github.css" media="screen, projection" rel="stylesheet" type="text/css">
<script>hljs.initHighlightingOnLoad();</script>
<script type="text/javascript">
function before_search(){
var searchVal = 'site:kaelinda.top ' + document.getElementById('search_input').value;
document.getElementById('search_q').value = searchVal;
return true;
}
</script>
</head>
<body class="antialiased hide-extras">
<div class="marketing off-canvas-wrap" data-offcanvas>
<div class="inner-wrap">
<nav class="top-bar docs-bar hide-for-small" data-topbar>
<section class="top-bar-section">
<div class="row">
<div style="position: relative;width:100%;"><div style="position: absolute; width:100%;">
<ul id="main-menu" class="left">
<li id=""><a target="_self" href="index.html">最近更新</a></li>
<li id=""><a target="_self" href="archives.html">归档目录</a></li>
<li id="ios.css"><a target="_self" href="ios.html">iOS</a></li>
<li id="swift.css"><a target="_self" href="swift.html">Swift</a></li>
<li id="reactnative.css"><a target="_self" href="reactnative.html">RN</a></li>
<li id="js.css"><a target="_self" href="js.html">JS</a></li>
<li id="shell.css"><a target="_self" href="shell.html">shell</a></li>
<li id="coding.css"><a target="_self" href="coding.html">编程</a></li>
<li id="live.css"><a target="_self" href="live.html">随笔</a></li>
<li id="app.css"><a target="_blank" href="app.html">APP推荐</a></li>
</ul>
<ul class="right" id="search-wrap">
<li>
<form target="_blank" onsubmit="return before_search();" action="http://google.com/search" method="get">
<input type="hidden" id="search_q" name="q" value="" />
<input tabindex="1" type="search" id="search_input" placeholder="Search"/>
</form>
</li>
</ul>
</div></div>
</div>
</section>
</nav>
<nav class="tab-bar show-for-small">
<a href="javascript:void(0)" class="left-off-canvas-toggle menu-icon">
<span> &nbsp; kaelinda</span>
</a>
</nav>
<aside class="left-off-canvas-menu">
<ul class="off-canvas-list">
<li><a target="_self" href="index.html">最近更新</a></li>
<li><a target="_self" href="archives.html">归档目录</a></li>
<li><a target="_self" href="ios.html">iOS</a></li>
<li><a target="_self" href="swift.html">Swift</a></li>
<li><a target="_self" href="reactnative.html">RN</a></li>
<li><a target="_self" href="js.html">JS</a></li>
<li><a target="_self" href="shell.html">shell</a></li>
<li><a target="_self" href="coding.html">编程</a></li>
<li><a target="_self" href="live.html">随笔</a></li>
<li><a target="_blank" href="app.html">APP推荐</a></li>
<li><label>Categories</label></li>
<li><a href="reactnative.html">React-Native</a></li>
<li><a href="ios.html">iOS</a></li>
<li><a href="app.html">MacAPP</a></li>
<li><a href="swift.html">Swift</a></li>
<li><a href="js.html">JavaScript</a></li>
<li><a href="vue.html">Vue</a></li>
<li><a href="shell.html">shell</a></li>
<li><a href="%E7%BD%91%E7%BB%9C.html">网络</a></li>
</ul>
</aside>
<a class="exit-off-canvas" href="#"></a>
<section id="main-content" role="main" class="scroll-container">
<script type="text/javascript">
$(function(){
$('#menu_item_index').addClass('is_active');
});
</script>
<div class="row">
<div class="large-8 medium-8 columns">
<div class="markdown-body article-wrap">
<div class="article">
<h1>ES6笔记(解构赋值)</h1>
<div class="read-more clearfix">
<span class="date">2017/12/20</span>
<span>posted in&nbsp;</span>
<span class="posted-in"><a href='js.html'>JavaScript</a></span>
<span class="comments">
</span>
</div>
</div><!-- article -->
<div class="article-content">
<h2 id="toc_0">解构赋值</h2>
<h3 id="toc_1">变量的解构:</h3>
<ul>
<li>解构赋值允许指定默认值。</li>
<li>解构赋值必须保证赋值方 被赋值方类型一直 但Item允许有遗漏或者多出。</li>
<li>默认值可以引用解构赋值的其他变量,但该变量必须已经声明</li>
</ul>
<pre><code class="language-text">let [x = 1, y = x] = []; // x=1; y=1
let [x = y, y = 1] = []; // ReferenceError
</code></pre>
<span id="more"></span><!-- more -->
<h3 id="toc_2">对象的 解构赋值:</h3>
<ul>
<li>对象的解构与数组有一个重要的不同。数组的元素是按次序排列的,变量的取值由它的位置决定;</li>
<li><p>而对象的属性没有次序,变量必须与属性同名,才能取到正确的值<br/>
(数据库、序列化)</p></li>
<li><p>对象的解构赋值的内部机制,是先找到同名属性,然后再赋给对应的变量。真正被赋值的是后者,而不是前者;前者是key,后者才是变量!</p>
<blockquote>
<p>注意,采用这种写法时,变量的声明和赋值是一体的。对于let和const来说,变量不能重新声明,所以一旦赋值的变量以前声明过,就会报错。</p>
</blockquote></li>
</ul>
<h3 id="toc_3">字符串的解构赋值</h3>
<blockquote>
<p>字符串也可以解构赋值。<br/>
这是因为此时,字符串被转换成了一个类似数组的对象<br/>
类似数组的对象都有一个length属性,因此还可以对这个属性解构赋值。</p>
</blockquote>
<pre><code class="language-javascript">let {length : len} = &#39;hello&#39;;
len // 5
let {toString: s} = 123;
s === Number.prototype.toString // true
let {toString: s} = true;
s === Boolean.prototype.toString // true
let { prop: x } = undefined; // TypeError
let { prop: y } = null; // TypeError
</code></pre>
<p>解构赋值时,如果等号右边是数值和布尔值,则会先转为对象。<br/>
undefined和null无法转为对象,所以对它们进行解构赋值,都会报错。</p>
<ul>
<li>不能使用圆括号的情况
<ul>
<li>变量声明语句中,不能带有圆括号</li>
<li>函数参数中,模式不能带有圆括号。</li>
<li>赋值语句中,不能将整个模式,或嵌套模式中的一层,放在圆括号之中</li>
</ul></li>
<li>可以使用圆括号的情况只有一种:赋值语句的<strong>非模式部分</strong>,可以使用圆括号。</li>
</ul>
</div>
<div class="row">
<div class="large-6 columns">
<p class="text-left" style="padding:15px 0px;">
<a href="15137627946753.html"
title="Previous Post: ES6 笔记(常量&&变量)">&laquo; ES6 笔记(常量&&变量)</a>
</p>
</div>
<div class="large-6 columns">
<p class="text-right" style="padding:15px 0px;">
<a href="15117133773045.html"
title="Next Post: 可选型 Optional">可选型 Optional &raquo;</a>
</p>
</div>
</div>
<div class="comments-wrap">
<div class="share-comments">
</div>
</div>
</div><!-- article-wrap -->
</div><!-- large 8 -->
<div class="large-4 medium-4 columns">
<div class="hide-for-small">
<div id="sidebar" class="sidebar">
<div id="site-info" class="site-info">
<div class="site-a-logo"><img src="https://ws1.sinaimg.cn/large/006tNc79ly1fmpk9088gsj30sg0sgn33.jpg" /></div>
<h1>kaelinda</h1>
<div class="site-des">倘若我心中的山水,你眼中能看到,我便一步一莲花祈祷</div>
<div class="social">
<a target="_blank" class="twitter" target="_blank" href="https://twitter.com/KaelLVLinda" title="Twitter">Twitter</a>
<a target="_blank" class="github" target="_blank" href="https://github.com/Kaelzzs" title="GitHub">GitHub</a>
<a target="_blank" class="email" href="mailto:zhouzuosong_kael@163.com" title="Email">Email</a>
<a target="_blank" class="rss" href="atom.xml" title="RSS">RSS</a>
</div>
</div>
<div id="site-categories" class="side-item ">
<div class="side-header">
<h2>Categories</h2>
</div>
<div class="side-content">
<p class="cat-list">
<a href="reactnative.html"><strong>React-Native</strong></a>
<a href="ios.html"><strong>iOS</strong></a>
<a href="app.html"><strong>MacAPP</strong></a>
<a href="swift.html"><strong>Swift</strong></a>
<a href="js.html"><strong>JavaScript</strong></a>
<a href="vue.html"><strong>Vue</strong></a>
<a href="shell.html"><strong>shell</strong></a>
<a href="%E7%BD%91%E7%BB%9C.html"><strong>网络</strong></a>
</p>
</div>
</div>
<div id="site-categories" class="side-item">
<div class="side-header">
<h2>Recent Posts</h2>
</div>
<div class="side-content">
<ul class="posts-list">
<li class="post">
<a href="15523077857608.html">OC中枚举写法 以及 字符串枚举探索</a>
</li>
<li class="post">
<a href="15522686353535.html">iOS 动画全解 (1)</a>
</li>
<li class="post">
<a href="15501356584731.html">UITableView顶部空白的几种解决办法</a>
</li>
<li class="post">
<a href="15488351784332.html">Objective-C中的字符串类型枚举 探索</a>
</li>
<li class="post">
<a href="15409691048860.html">【Swift笔记】引用计数</a>
</li>
</ul>
</div>
</div>
</div><!-- sidebar -->
</div><!-- hide for small -->
</div><!-- large 4 -->
</div><!-- row -->
<div class="page-bottom clearfix">
<div class="row">
<p class="copyright">Copyright &copy; 2015
Powered by <a target="_blank" href="http://www.mweb.im">MWeb</a>,&nbsp;
Theme used <a target="_blank" href="http://github.com">GitHub CSS</a>.</p>
</div>
</div>
</section>
</div>
</div>
<script src="asset/js/foundation.min.js"></script>
<script>
$(document).foundation();
function fixSidebarHeight(){
var w1 = $('.markdown-body').height();
var w2 = $('#sidebar').height();
if (w1 > w2) { $('#sidebar').height(w1); };
}
$(function(){
fixSidebarHeight();
})
$(window).load(function(){
fixSidebarHeight();
});
</script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><script type="text/x-mathjax-config">MathJax.Hub.Config({TeX: { equationNumbers: { autoNumber: "AMS" } }});</script>
</body>
</html>
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/kaelinda/kaelindaTop.git
git@gitee.com:kaelinda/kaelindaTop.git
kaelinda
kaelindaTop
kaelindaTop
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385