Family of Nicky and Cookie
'use strict'
let updateFunctions =[];
for(var i=0;i<2;++i){
updateFunctions.push(function(){return i;})
}
console.log(updateFunctions[0]());//2
'use strict'
let updateFunctions =[];
for(let i=0;i<2;++i){
updateFunctions.push(function(){return i;})
}
console.log(updateFunctions[0]());//0
'use strict'
var i =12;
if(true){
var i=2000;
}
console.log(i);//2000
'use strict'
let i =12;
if(true){
let i=2000;
}
console.log(i);//12
'use strict'
document.addEventListener('click', function(){
console.log(this);//#document
});
'use strict'
document.addEventListener('click', ()=> console.log(this));// window {...}
//用了array function,this不是出发event的element了,而是run这个code的context
'use strict'
var invoice ={
number:123,
process:function(){
console.log(this);
}
};
invoice.process();//object-invoice
'use strict'
var invoice ={
number:123,
process:()=>console.log(this)
};
invoice.process();//window
'use strict'
var invoice ={
number:123,
process:function(){
return ()=>console.log(this.number);
}
};
invoice.process()();//123
'use strict'
var invoice ={
number:123,
process:function(){
return ()=>console.log(this.number);
}
};
var newInvoice={
number:456
}
invoice.process().bind(newInvoice)();//123
//array function不能用bind去改变this的值,这种方式尽管没有throw exception
'use strict'
var invoice ={
number:123,
process:function(){
return ()=>console.log(this.number);
}
};
var newInvoice={
number:456
}
invoice.process().call(newInvoice);//123
//array function不能用bind去改变this的值,这种方式尽管没有throw exception
//call, apply, bind 对于array function都是无意义的
'use strict'
var getPrice = ()=>5.99;
console.log(getPrice.hasOwnProperty('prototype'));//false
'use strict'
var getTotal = function(price, tax=price*0.07){
console.log(price+tax);
}
console.log(getTotal(5.00));//5.35
'use strict'
var getTotal = function(price, tax=0.07){
console.log(arguments.length);//1
}
'use strict'
var showCategories = function(productId, ...categories){
console.log(categories instanceof Array);//true
console.log(categories);//['search','advertisement']
};
showCategories(123, 'search', 'advertisement');
'use strict'
var prices = [12,20,18];
var maxPrice = Math.max(...prices);//Math.max(12,20,18)
console.log(maxPrice);
'use strict'
var price =5.99, quantity=10;
var productView={
price,
quantity
};
console.log(productView);//{price:5.99,quantity:10}
'use strict'
var price =5.99, quantity=10;
var productView={
price,
quantity,
calc(){//不需要再写calc:function(){}
return this.price*this.quantity;
}
};
console.log(productView.calc());//59.9
'use strict'
var ident = 'productId';
var productView = {
get[ident](){return true},
set[ident](value){}
//http://whereswalden.com/2010/08/22/incompatible-es5-change-literal-getter-and-setter-functions-must-now-have-exactly-zero-or-one-arguments/
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set
};
console.log(productView.productId);//true
'use strict'
var categories=[,,1];
for(var item of categories){
console.log(item);
}
//undefined
//undefined
//1
'use strict'
var codes = 'ABCDF';
var count =0;
for(var code of codes){
count++;
}
console.log(count);//5
'use strict'
function processInvoice(segments){
console.log(segments);
}
processInvoice `template`;//['template']
'use strict'
function processInvoice(segments, ...values){
console.log(segments);//["Invoice: ", " for ", "", raw: Array[3]]
console.log(values);//["1350", "2000"]
}
let invoiceNum='1350';
let amount = '2000';
processInvoice `Invoice: ${invoiceNum} for ${amount}`;
'use strict'
let salary = ['32000','50000','75000'];
let [low,average,high]=salary;
console.log(average);//50000不是字符串
'use strict'
let salary = ['32000','50000'];
let [low,average,high]=salary;
console.log(high);//undefined
'use strict'
let salary = ['32000','50000','75000'];
let [low,,high]=salary;
console.log(high);//75000
'use strict'
let salary = ['32000','50000','75000'];
let [low,...remaining]=salary;
console.log(remaining);//["50000", "75000"]