Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@pjchender
Last active August 8, 2018 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjchender/77975754ec5066deff22433ad3ec16d2 to your computer and use it in GitHub Desktop.
Save pjchender/77975754ec5066deff22433ad3ec16d2 to your computer and use it in GitHub Desktop.
[AC] Object creating and constructor function
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']
}
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'])
let colorBook = {}
colorBook.red = '#FF0000'
colorBook.blue = '#0000FF'
colorBook.green = '#00FF00'
let color = 'blue'
colorBook.color
colorBook['color']
colorBook[color]
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