实现一个LazyMan,可以按照以下方式调用: LazyMan("Hank")输出: Hi! This is Hank! LazyMan("Hank").sleep(10).eat("dinner") 输出 Hi! This is Hank! //等待10秒.. Wake up after 10 Eat dinner~ LazyMan("Hank").eat("dinner").eat("supper") 输出 Hi This is Hank! Eat dinner~ Eat supper~ LazyMan("Hank").sleepFirst(5).eat("supper") 输出 //等待5秒 Wake up after 5 Hi This is Hank! Eat supper 以此类推
/* [UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "1".] { code: 'ERR_UNHANDLED_REJECTION' } */
// A // / \ // B C // /\ /\ // D E F G let A = newPromise((resolve, reject) => { console.log('A'); resolve(); });
let B = A.then(() =>console.log('B')); let C = A.then(() =>console.log('C'));
B.then(() =>console.log('D')); B.then(() =>console.log('E')); C.then(() =>console.log('F')); C.then(() =>console.log('G')); // A // B // C // D // E // F // G
// 这两种类型没有实现迭代器工厂函数 console.log(num[Symbol.iterator]); // undefined console.log(obj[Symbol.iterator]); // undefined let str = 'abc'; let arr = ['a', 'b', 'c']; let map = newMap().set('a', 1).set('b', 2).set('c', 3); let set = newSet().add('a').add('b').add('c'); let els = document.querySelectorAll('div');
// 这些类型都实现了迭代器工厂函数 console.log(str[Symbol.iterator]); // f values() { [native code] } console.log(arr[Symbol.iterator]); // f values() { [native code] } console.log(map[Symbol.iterator]); // f values() { [native code] } console.log(set[Symbol.iterator]); // f values() { [native code] } console.log(els[Symbol.iterator]); // f values() { [native code] }
classPerson { sayHelloPorototype(name) { console.log(`${name} say hello, ${this}`); } staticsayHelloClass(name) { console.log(`${name} say hello, ${this}`); } }
classWomenextendsPerson{}
let p1 = newPerson(); let w1 = newWomen();
p1.sayHelloPorototype('Katrina'); // Katrina say hello, Person{} w1.sayHelloPorototype('Jack'); // Jack say hello, Women{}
Person.sayHelloClass('Katrina1'); // Katrina1 say hello, class Person {} Women.sayHelloClass('Jack1'); // Jack1 say hello, class Women extends Person{}
extends也可以在类表达式中使用
1 2 3 4 5 6 7 8 9 10 11
classPerson { sayHelloPorototype(name) { console.log(`${name} say hello, ${this}`); } staticsayHelloClass(name) { console.log(`${name} say hello, ${this}`); } }
classBusextendsVehicle { constructor() { console.log(this); } } newBus(); // ReferenceError: Must call super constructor in derived class // before accessing 'this' or returning from derived constructor
console.log(newCar()); // Car {} console.log(newBus()); // Bus {} console.log(newVan()); // {} console.log(newJeep()); // Uncaught ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor