JavaScript Tutorial/Object Oriented/Combination

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

Class combination with constructor

<html>
<head>
<script language="Javascript" type = "text/javascript">
<!--
function companyObj (cname, caddress, cphone) {
    this.cname = cname;
    this.caddress = caddress;
    this.ctelephone = cphone;
}
var newComp = new companyObj("ABC", "City, State", "555-555-5555");
function EmployeeObj (name, address, phone, email,newComp) {
    this.name = name;
    this.address = address;
    this.telephone = phone;
    this.emailaddress = email;
    this.rupObj = newComp;
}
var newCust = new EmployeeObj("J S", "City, State", "555-555-5555","j@s.ru", newComp);
document.write("Employee Name:    " + newCust.name + "<br>\n");
document.write("Address:          " + newCust.address + "<br>\n");
document.write("Telephone Number: " + newCust.telephone + "<br>\n");
document.write("Email Address:    " + newCust.emailaddress + "<br>\n");
document.write("Company Name:     " + newCust.rupObj.cname + "<br>\n");
document.write("Company Address:  " + newCust.rupObj.caddress + "<br>\n");
document.write("Telephone Number: " + newCust.rupObj.ctelephone);
//-->
</script>
</head>
<body>
</body>
</html>


Class combination with prototype

<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>