var person = {};
var objectName = {
member1Name: member1Value,
member2Name: member2Value,
member3Name: member3Value
}
e.g.
var person = {
name: ['Bob', 'Smith'],
age: 32,
gender: 'male',
interests: ['music', 'skiing'],
bio: function() {
alert(this.name[0] + ' ' + this.name[1] + ' is ' + this.age + ' years old. He likes ' + this.interests[0] + ' and ' + this.interests[1] + '.');
},
greeting: function() {
alert('Hi! I\'m ' + this.name[0] + '.');
}
};
person.name[0]
person.age
person.bio()
person.greeting()
- We can access the object's properties and methods using dot notation.
e.g.
person.age
person.interests[1]
person.bio()
1. Dot Notation
- We can access the object's properties and methods using dot notation.
e.g.
person.age
person.interests[1]
person.bio()
2. Sub-namespaces
- It is even possible to make the value of an object member another object.
name: ['Bob', 'Smith'],
e.g.
name : {
first: 'Bob',
last: 'Smith'
}
person.name.first
person.name.last
3. Bracket Notation
- Useful for accessing or setting value or property dynamically
person['age']
person['name']['first']
Setting object members:
person.age = 45
person['name']['last'] = 'Cratchit'
person['eyes'] = 'hazel'
var myDataName = 'height'
var myDataValue = '1.75m'
person[myDataName] = myDataValue
var person1 = {
name: 'Chris',
greeting: function() {
alert('Hi! I\'m ' + this.name + '.');
}
}
var person2 = {
name: 'Brian',
greeting: function() {
alert('Hi! I\'m ' + this.name + '.');
}
}
//Object Literal: function returns a new object
function createNewPerson(name) {
var obj = {};
obj.name = name;
obj.greeting = function() {
alert('Hi! I\'m ' + this.name + '.');
};
return obj;
}
var salva = createNewPerson('Salva');
salva.name;
salva.greeting();
______________________________________________
//Constructor Example
function Person(name) {
this.name = name;
this.greeting = function() {
alert('Hi! I\'m ' + this.name + '.');
};
}
var person1 = new Person('Bob');
person1.greeting();
var person1 = new Object();
person1.name = 'Chris';
person1['age'] = 38;
person1.greeting = function() {
alert('Hi! I\'m ' + this.name + '.');
};
/** OR **/
var person1 = new Object({
name: 'Chris',
age: 38,
greeting: function() {
alert('Hi! I\'m ' + this.name + '.');
}
});
- generic objects have a constructor, which generates an empty object.
var person2 = Object.create(person1);
person2.name
person2.greeting()
// Check constructor part
person1.constructor
person2.constructor
- create a new object instance based on an existing object.
function Person(first, last, age, gender, interests) {
// property and method definitions
};
var person1 = new Person('Tammi', 'Smith', 32, 'neutral', ['music', 'skiing']);
Person.prototype.farewell = function() {
alert(this.name.first + ' has left the building. Bye for now!');
}
person1.farewell();
Modifying prototypes
function Teacher(first, last, age, gender, interests, subject) {
Person.call(this, first, last, age, gender, interests);
this.subject = subject;
}
// We need to get Teacher() to inherit the methods defined on Person()'s prototype
Teacher.prototype = Object.create(Person.prototype);
// Constructor is changed to Person, Change constructor back to Teacher
Teacher.prototype.constructor = Teacher;
Inheritance