Because most of the projects in the group involve javascript, css, and other clients are under the responsibility of another colleague. This colleague is very busy and cannot be reconstructed, so the boss just proposes suggestions and does not implement reconstruction immediately. But I also changed some bugs on the client some days ago. It is true that the Code is a bit cloudified, and I don't know where the code is, so I don't dare to touch the code easily, so I started to drum up the javascript that I once loved and hated, and wrote a simple js implementation namespace, inheritance, heavy load and other object-oriented features. welcome to tile Bumping
. Define namespace
Namesapce. js
Copy codeThe Code is as follows:
Namespace = new Object ();
Namespace. register = function (fullname ){
Try
{
Var nsArray = fullname. split (".");
Var strNS = "";
Var strEval = "";
For (var I = 0; I <nsArray. length; I ++ ){
If (strNS. length> 0)
StrNS + = ".";
StrNS + = nsArray [I];
StrEval + = "if (typeof (" + strNS + ") = 'undefined')" + strNS + "= new Object ();";
}
If (strEval! = "") Eval (strEval );
} Catch (e) {alert (e. message );}
}
. Employee. js
Employee. js
Copy codeThe Code is as follows:
// Register the namespace
Namespace. register ("MyCompany ");
// 1. Category: Employee
MyCompany. Employee = function (empName ){
This. Name = empName;
This. salary= 1000;
This. Position = "cleaner ";
}
MyCompany. Employee. prototype. ShowName = function (){
Return "I'm" + this. Name + ", my salary is $" + this. Salary;
}
MyCompany. Employee. prototype. Work = function (){
Return "I'm a" + this. Position + ", I'm cleaning all day! "
}
// 2. Category: programmer
MyCompany. Developer = function (empName ){
// Inherit the attributes of the parent class
MyCompany. Employee. call (this, empName );
// Overwrite the attributes of the parent class
This. Position = "developer ";
// Extended attributes
This. Technology = "C #";
}
// Inherit the parent class Prototype Method
MyCompany. Developer. prototype = new MyCompany. Employee ();
// Override the parent class Method
MyCompany. Developer. prototype. Work = function (){
Return "I'm a" + this. Position + ", I'm good at" + this. Technology + ", I'm coding all day! "
}
Test code
Copy codeThe Code is as follows:
<Html>
<Title> javascript object-oriented implementation namespace, class, inheritance, and overloading </title>
<Head>
<Script language = "javascript" src = "namespace. js" type = "text/javascript"> </script>
<Script language = "javascript" src = "employee. js" type = "text/javascript"> </script>
<Script language = "javascript" type = "text/javascript">
Function showEmployee (){
Var emp = new MyCompany. Employee ("xiaoming ");
ShowInfo (emp );
}
Function showDeveloper (){
Var emp = new MyCompany. Developer ("Gates ");
ShowInfo (emp );
}
Function showInfo (emp ){
Alert (emp. ShowName ());
Alert (emp. Work ());
}
</Script>
</Head>
<Body>
<Input type = "button" value = "show employee info" onclick = "showEmployee ();"/> <br/>
<Input type = "button" value = "show developer info" onclick = "showDeveloper ();"/>
</Body>
</Html>
Package and download source code