JavaScript Tutorial/Object Oriented/Combination

Материал из Web эксперт
Версия от 11:24, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Class combination with constructor

   <source lang="javascript">

<html> <head> <script language="Javascript" type = "text/javascript">

</script> </head> <body> </body> </html></source>


Class combination with prototype

   <source lang="javascript">

<html> <head> <title>Example</title> </head> <body> <script type="text/javascript"> function BaseClass(sColor) {

   this.color = sColor;

} BaseClass.prototype.sayColor = function () {

   alert(this.color);

}; function SubClass(sColor, sName) {

   BaseClass.call(this, sColor);
   this.name = sName;

} SubClass.prototype = new BaseClass(); SubClass.prototype.sayName = function () {

   alert(this.name);

};

var objA = new BaseClass("red"); var objB = new SubClass("blue", "MyName"); objA.sayColor(); objB.sayColor(); objB.sayName(); </script> </body> </html></source>