JavaScript 物件導向介紹

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
  1. Define the object type by writing a function
  2. create an instance of the object with new

when new Foo(...) is executed

  1. A new object is created, inheriting from Foo.prototype
  2. 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

  1. add new properties to an existing prototype
  2. 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;

};

results matching ""

    No results matching ""