mirror of
https://github.com/ruanyf/es6tutorial.git
synced 2025-05-24 10:22:23 +00:00
edit ditto.js
This commit is contained in:
parent
92d99645f8
commit
022a65ee7b
@ -18,6 +18,25 @@ var p = new Point();
|
||||
|
||||
任何带有默认值的参数,被视为可选参数。不带默认值的参数,则被视为必需参数。
|
||||
|
||||
利用参数默认值,可以指定一个参数不得省略,如果省略就抛出一个错误。
|
||||
|
||||
```javascript
|
||||
|
||||
function throwIfMissing() {
|
||||
throw new Error('Missing parameter');
|
||||
}
|
||||
|
||||
function foo(mustBeProvided = throwIfMissing()) {
|
||||
return mustBeProvided;
|
||||
}
|
||||
|
||||
foo()
|
||||
// Error: Missing parameter
|
||||
|
||||
```
|
||||
|
||||
上面代码的foo函数,如果调用的时候没有参数,就会调用默认值throwIfMissing函数,从而抛出一个错误。
|
||||
|
||||
## rest(...)运算符
|
||||
|
||||
ES6引入rest运算符(...),用于获取函数的多余参数,这样就不需要使用arguments.length了。rest运算符后面是一个数组变量,该变量将多余的参数放入数组中。
|
||||
|
@ -197,6 +197,10 @@ proxy.title // 35
|
||||
|
||||
```javascript
|
||||
|
||||
var person = {
|
||||
name: "张三"
|
||||
};
|
||||
|
||||
var proxy = new Proxy(target, {
|
||||
get: function(target, property) {
|
||||
if (property in target) {
|
||||
@ -207,6 +211,9 @@ var proxy = new Proxy(target, {
|
||||
}
|
||||
});
|
||||
|
||||
proxy.name // "张三"
|
||||
proxy.age // 抛出一个错误
|
||||
|
||||
```
|
||||
|
||||
上面代码表示,如果访问目标对象不存在的属性,会抛出一个错误。如果没有这个拦截函数,访问不存在的属性,只会返回undefined。
|
||||
|
@ -16,6 +16,8 @@
|
||||
- Nick Fitzgerald, [Destructuring Assignment in ECMAScript 6](http://fitzgeraldnick.com/weblog/50/)
|
||||
- Nicholas C. Zakas, [Understanding ECMAScript 6 arrow functions](http://www.nczonline.net/blog/2013/09/10/understanding-ecmascript-6-arrow-functions/)
|
||||
- Jack Franklin, [Real Life ES6 - Arrow Functions](http://javascriptplayground.com/blog/2014/04/real-life-es6-arrow-fn/)
|
||||
- Axel Rauschmayer, [Handling required parameters in ECMAScript 6](http://www.2ality.com/2014/04/required-parameters-es6.html)
|
||||
- Nicholas C. Zakas, [Creating defensive objects with ES6 proxies](http://www.nczonline.net/blog/2014/04/22/creating-defensive-objects-with-es6-proxies/)
|
||||
|
||||
## Generator
|
||||
|
||||
|
@ -200,7 +200,8 @@ function router() {
|
||||
Prism.highlightElement(this);
|
||||
});
|
||||
|
||||
function loadDisqus() {
|
||||
// 加载disqus
|
||||
(function () {
|
||||
// http://docs.disqus.com/help/2/
|
||||
window.disqus_shortname = 'es6';
|
||||
window.disqus_identifier = (location.hash?location.hash.replace("#", ""):'READEME');
|
||||
@ -210,11 +211,10 @@ function router() {
|
||||
// http://docs.disqus.com/developers/universal/
|
||||
(function() {
|
||||
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
|
||||
dsq.src = 'http://es6.disqus.com/embed.js';
|
||||
dsq.src = 'http://'+window.disqus_shortname+'.disqus.com/embed.js';
|
||||
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
|
||||
})();
|
||||
}
|
||||
loadDisqus();
|
||||
})();
|
||||
|
||||
if(path.indexOf('README') === -1){
|
||||
$('html, body').animate({
|
||||
|
Loading…
x
Reference in New Issue
Block a user