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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| const bookLiteral = { bookName : 'Program', bookPrice : '30', calcPrice : function(){} };
function createBook(bookName, bookPrice) { return { bookName, bookPrice, calcPrice :function (numOfBooks){ return this.bookPrice * numOfBooks; } } }
bookFromFactory = createBook("Factory Object", 15); console.log(bookFromFactory); console.log(bookFromFactory.calcPrice(30));
function Book(bookName, bookPrice){ this.bookName = bookName; this.bookPrice = bookPrice; this.calcPrice = function (numOfBooks){ return this.bookPrice * numOfBooks; } }
myBook = new Book('JS', 10); console.log(myBook.constructor);
for (let key in myBook){ console.log(key, myBook[key]); }
const keys = Object.keys(myBook); console.log(keys);
if ('bookPrice' in myBook) { console.log('the price of 10 books: ', myBook.calcPrice(10)); }else console.log('price of book:', myBook.calcPrice(1));
function Circle(radius){ this.radius = radius; let defaultLocation = {x:10, y:10}; this.draw = function (){ console.log('DefaultLocation:', defaultLocation); }; Object.defineProperty(this, 'defaultLocation', { get: function(){ return defaultLocation; }, set: function(value){ if (!value.x || !value.y){ throw new Error('Not Valid Value'); } defaultLocation = value; } }); }
var circle = new Circle(10);
circle.defaultLocation = {x:3, y:5}; console.log(circle.defaultLocation);
|