1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | let bookOrder = {'isbn': '978-0375504525', 'qty': 1}; function processBookOrders() { getNextOrder() .then((order) => doWeCarryThisBook(order)) .then((order) => isItInStock(order)) .catch((err) => console.log(err.message)); } processBookOrders(); function getNextOrder() { let promise = new Promise( function(resolve, reject) { console.log('Getting the next order'); // pretend this is an async call to an orders database setTimeout(function() { let success = true; if (success) resolve(bookOrder); else reject(Error('No more orders!')); }, 1000); } ); return promise; } function doWeCarryThisBook(order) { let promise = new Promise( function(resolve, reject) { console.log(`Looking for ISBN ${order.isbn}`); // pretend this is an async call to a books database setTimeout(function() { if (order.isbn == '978-0375504525') { console.log('The book title is \'As I Lay Dying.\''); resolve(order); } else { reject(Error('This is not a book we carry.')); } }, 1000); } ); return promise; } function isItInStock(order) { let promise = new Promise( function(resolve, reject) { // pretend this is an async call to check stock levels setTimeout(function() { if (order.qty == 1) { console.log('The book is in stock!'); resolve(order); } else { reject(Error('We do not have enough copies of the book in stock.')); } }, 1000); } ); return promise; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | var firstNameS = Symbol('firstName'); var lastNameS = Symbol('lastName'); var ageS = Symbol('age'); class Person { constructor(firstName, lastName, age) { this[firstNameS] = firstName; this[lastNameS] = lastName; this[ageS] = age; } }; var ted = new Person('Ted', 'Neward', 45); ted.sayHowdy = function() { console.log(this[firstNameS] + ' says howdy!'); }; ted.waveGoodbye = function(msg) { return '' + msg + ' Buh-bye!'; }; ted.sayHowdy(); console.log(ted.waveGoodbye('See you next time!')); |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |