Q. new operator
The new operator creates an instance of a object that has a constructor function
new constructor[([arguments])]
constructor
A function that specifies the type of the object instance
Q.Creating a user-defined object
- Define the object type by writing a function
- create an instance of the object with new
when new Foo(...)
is executed
- A new object is created, inheriting from Foo.prototype
- this keyword bound to the newly created object
you can always add a property to a previously defined object,
car1.color = 'black'
Q. Prototype property
The javascript prototype property allows you
- add new properties to an existing prototype
- add new methods to an existing prototype
//建構子
function Ancestor() {
this.setBirthdayYear(1889);
}
//屬性
Ancestor.prototype.birthday = {
year: null,
month: null,
day: null
};
//方法
Ancestor.prototype.setBirthdayYear = function (year)
{
this.birthday.year = year;
};