Last active
August 8, 2018 15:34
-
-
Save pjchender/77975754ec5066deff22433ad3ec16d2 to your computer and use it in GitHub Desktop.
[AC] Object creating and constructor function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let alphaPhoneX = { | |
name: 'AlphaPhoneX', // String | |
price: 14999, // Number | |
features: ['long battery life', 'AI camera'], // Array | |
showSpec: function() { // Function | |
console.log('show the spec of the phone') | |
}, | |
hardware: { // Object | |
ram: '8GB', | |
storage: '64GB' | |
} | |
} | |
let alphaPhoneY = { | |
name: 'AlphaPhoneY', | |
price: 18900, | |
features: ['water proof', 'high screen resolution'] | |
} | |
let alphaPhoneZ = { | |
name: 'AlphaPhoneZ', | |
price: 23900, | |
features: ['IP47', 'high screen resolution', 'full display'] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function SmartPhone(name, price, features){ | |
this.name = name | |
this.price = price | |
this.features = features | |
this.showPhoneInfo = function() { | |
console.log(`The price of ${this.name} is $${this.price}, which has the newest features such as ${this.features.join(', ')}.`) | |
} | |
} | |
let alphaPhoneX = new SmartPhone('alphaPhoneX', 14999, ['long battery life', 'AI camera']) | |
let alphaPhoneY = new SmartPhone('alphaPhoneY', 18900, ['water proof', 'high screen resolution']) | |
let alphaPhoneZ = new SmartPhone('alphaPhoneZ', 23900, ['IP47', 'high screen resolution', 'full display']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let colorBook = {} | |
colorBook.red = '#FF0000' | |
colorBook.blue = '#0000FF' | |
colorBook.green = '#00FF00' | |
let color = 'blue' | |
colorBook.color | |
colorBook['color'] | |
colorBook[color] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let alphaPhoneX = {} | |
// dot-notation | |
alphaPhoneX.name = 'alphaPhoneX' | |
alphaPhoneX.price = 14999 | |
alphaPhoneX.features = ['long battery life', 'AI camera'] | |
alphaPhoneX.showSpec = function() { | |
console.log('show the spec of the phone') | |
} | |
alphaPhoneX.hardware = { | |
ram: '8GB', | |
storage: '64GB' | |
} | |
// bracket-notation | |
alphaPhoneX['name'] = 'alphaPhoneX' | |
alphaPhoneX['price'] = 14999 | |
alphaPhoneX['features'] = ['long battery life', 'AI camera'] | |
alphaPhoneX['showSpec'] = function() { | |
console.log('show the spec of the phone') | |
} | |
alphaPhoneX['hardware'] = { | |
ram: '8GB', | |
storage: '64GB' | |
} | |
// object literal syntax | |
alphaPhoneX = { | |
name: 'AlphaPhoneX', // String | |
price: 14999, // Number | |
features: ['long battery life', 'AI camera'], // Array | |
showSpec: function() { // Function | |
console.log('show the spec of the phone') | |
}, | |
hardware: { // Object | |
ram: '8GB', | |
storage: '64GB' | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment