Skip to content

Instantly share code, notes, and snippets.

@toropanov
Last active August 29, 2015 14:15
Show Gist options
  • Save toropanov/6ba63eb1e39484b23d9b to your computer and use it in GitHub Desktop.
Save toropanov/6ba63eb1e39484b23d9b to your computer and use it in GitHub Desktop.
Working with Objects
// Create empty object
var mikhail = Object.create(null)
// Updating values
mikhail['name'] = 'Mikhail'
// or also could be like this
mikhail.name = 'Mikhail'
// Deleting property in object
delete mikhail['gender']
// Adding to existing objects объекту
Object.defineProperties(array_name, { name: { value: 'Value'
, writable: true
, configurable: true
, enumerable: true }
// As a value in object could be also used function
var mikhail = { name: 'Mikhail'
, get name() {
return this.first_name + ' ' + this.last_name }
, set name(new_name) { var names
names = new_name.trim().split(/\s+/)
this.first_name = names['0'] || ''
this.last_name = names['1'] || '' }
}
// One more example with function in object
function add(other, yet_another) {
return this.value + other + (yet_another || 0)
}
var one = { value: 1, add: add }
// Creating protype
var mikhail = Object.create(person)
function Class(){/*тут инициализируем поля *}
Class.prototype = {/*Методы*/}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment