本文共 9048 字,大约阅读时间需要 30 分钟。
//obj instanceof Array == Array[Symbol.hasInstance](obj)function specailNumber(){}Object.defineProperty(specailNumber,Symbol.hasInstance,{value:(v)=>(v instanceof Number) && (v >= 1 && v <= 100)})var two = new Number(2) zero = new Number(0)print(two instanceof specailNumber)print(zero instanceof specailNumber)-------------------------------------------------------------------let collection = { 0:"hello", 1:"world", length:2, [Symbol.isConcatSpreadable]:true};let message = ["HI"].concat(collection);print(message) //[ 'HI', 'hello', 'world' ]
typeof symbol
let firstname = Symbol("firstname");//可计算let person = { [firstname]:"Nichols"};//将属性设置为只读Object.defineProperty(person,firstname,{writable:false});let lastname = Symbol("last name");Object.defineProperties(person,{ [lastname]:{ value:"Zakes", writable:false }});print(person.firstname,person.lastname);
但是可以在箭头函数上调用call apply bind方法,与其他函数不同,箭头函数的this值不会受这些方法的影响
var sum = (num1,num2) => num1+num2;print(sum.call(null,1,2))print(sum.apply(null,[1,2]))var boundSum = sum.bind(null,1,2)print(boundSum())
普通对象
特异对象
标准对象
内建对象
var obj = { a:1, 0:1, c:1, 2:1, b:1, 1:1};obj.d = 1;print(Object.getOwnPropertyNames(obj).join("")) //012acbd
let person = { getGreeting(){ return "hello"; }}let dog = { getGreeting(){ return "woof!"; }}//person prototypelet friend = Object.create(person);print(friend.getGreeting()); //helloprint(Object.getPrototypeOf(friend) == person); //true//set prototypeOf dogObject.setPrototypeOf(friend,dog);print(friend.getGreeting()) //woof!print(Object.getPrototypeOf(friend) == dog); //true
let person = { getGreeting(){ return "hello"; }}let dog = { getGreeting(){ return "woof!"; }}let friend = { getGreeting(){ //return Object.getPrototypeOf(this).getGreeting.call(this) +" hi!"; return super.getGreeting() +" hi!"; }}Object.setPrototypeOf(friend,person);print(friend.getGreeting())print(Object.getPrototypeOf(friend) === person)Object.setPrototypeOf(friend,dog);print(friend.getGreeting())print(Object.getPrototypeOf(friend) == dog)
let node = { type:"Identity", name:"foo", loc:{ start:{ line:1, column:1 }, end:{ line:2, column:2 } }}let {loc :{end}} = node ;print(end.line,end.column)let a = 1, b = 2 ;[a,b] = [b,a]print(a,b) //交换 a,b的值// 复制数组let colors = ["red","green","blue"];let [...clone] = colorsprint(clone)print(colors.join(" "))print(colors.concat(" "))//应用场景1function setCookie(name,value,{secure,path,domain,expire} = {}) //使用解构的话比较清晰// ES6 之前的模拟 set and map var set = Object.create(null);set.foo = true if(set.foo){ print("hello")}var map = Object.create(null)map.foo = "bar";print(map.foo)// 若引用set, 集合中的弱引用如果是对象的唯一引用,则会被回收并且释放相应内存(不支持原始值,只支持引用类型)let set = new WeakSet(), key = {};set.add(key);print(set.has(key)); //trueset.delete(key);print(set.has(key)); //false//注意weakset: 不可迭代即不可以使用for..in (2)不能不报露任何迭代器例如keys and values (3)不支持forEach (4) 不支持size属性// set and map底层都是Object.is判断相等的 let map = new Map();map.set("title","Understanding js");map.set("year",2016)print(map.get("year"))print(map.get("title"))//默认初始化let map = new Map([["title","Understanding js"],["year",2016]]);//迭代map (value,key,ownermap) => print ... map.forEach((v,k) => print(k+" -> "+v))//使用weak map 存储私有数据let Person = (function(){ let privateDate = new WeakMap(); function Person(name){ privateDate.set(this,{name:name}); } Person.prototype.getName = function(){ return privateDate.get(this).name; }; return Person;}());var p1 = new Person("shen");var p2 = new Person("yang");print(p1.getName())print(p2.getName())//新的迭代器: for ..of 循环,展开运算符(...) ,异步编程也可以使用迭代器//ES5中写迭代器的写法, ES6中增加了 一个生成器对象,他可以让创建迭代器对象变得简单function createIter(items){ var i = 0; return { next : function(){ var done = (i>=items.length); var value = !done ? items[i++] : undefined; return {done:done,value:value}; } }}var iter = createIter([1,2,3])print(iter.next())print(iter.next())print(iter.next())print(iter.next())//生成器是一种返回迭代器的函数,通过function*来表示,函数中会用到新的关键字yieldfunction*createIter(){ yield 1; yield 2; yield 3;}function*createIter(items){ for(let i = 0;i"+value);}//第二次调用next()方法传入的值为4,他会被赋值给变量first,函数继续执行function *createIter(){ let first = yield 1; let second = yield first + 2; yield second + 3;}let iter = createIter();print(iter.next());print(iter.next(4));print(iter.next(5));print(iter.next());//yield语句也可以抛出异常function *createIter(){ let first = yield 1; let second; try{ second = yield first + 2; }catch(ex){ second = 6; } yield second +3;}let iter = createIter();print(iter.next()) // { value: 1, done: false }print(iter.next(4)) // { value: 6, done: false }print(iter.throw(new Error("Boom!"))) //{ value: 9, done: false }print(iter.next()) //{ value: undefined, done: true }// yield 遇到 return 也会提前返回// 在某些情况下,需要两个迭代器合二为一,这时可以创建一个生成器,再给yield语句添加一个星号,就可以将生成数据的过程委托给其他生成器.function *createNumber(){ yield 1; yield 2;}function *createColor(){ yield "red"; yield "green";}function *createCombinIter(){ yield *createNumber(); yield *createColor(); yield true;}var iter = createCombinIter();print(iter.next()) //{ value: 1, done: false }print(iter.next()) //{ value: 2, done: false }print(iter.next())//{ value: 'red', done: false }print(iter.next())//{ value: 'green', done: false }print(iter.next())//{ value: true, done: false }print(iter.next())//{ value: undefined, done: true }//简单任务执行器: 由于执行yield语句会暂停当前函数的执行过程并等待下一次调用next()方法,因此可以创建一个函数,在函数中调用生成器生成相应的迭代器,从而在不用回调函数的基础上实现异步调用next方法function run(taskDef){ //创建一个无使用限制的迭代器 let task = taskDef(); let result = task.next(); //循环调用next函数 function step(){ //如果任务没完成,则继续完成 if(!result.done){ result = task.next() step() } } //开始迭代 step();}run(function*(){ print("1..."); yield; print("2 ..."); yield; print("3 ... ");});//任务执行器之间互相通信function run(taskDef){ //创建一个无使用限制的迭代器 let task = taskDef(); let result = task.next(); //循环调用next函数 function step(){ //如果任务没完成,则继续完成 if(!result.done){ result = task.next(result.value) step() } } //开始迭代 step();}run(function*(){ let value = yield 1; print("value: "+value); // 1 value = yield value + 3 print("value: "+value) // 4//如果result.value是一个函数function run(taskDef){ //创建一个无使用限制的迭代器 let task = taskDef(); let result = task.next(); //循环调用next函数 function step(){ //如果任务没完成,则继续完成 if(!result.done){ if(typeof result.value == "function"){ result.value(function(err,data){ if(err){ result = task.throw(err); return; } result = task.next(data); step(); }) }else{ result = task.next(result.value); step(); } } } //开始迭代 step();}let fs = require("fs");function readFile(filename){ return function(callback){ fs.readFile(filename,callback); };}run(function*(){ let contents = yield readFile("data.txt"); print(String(contents)); print("Done");});
转载地址:http://srvjo.baihongyu.com/