Copy codeThe Code is as follows:
<Script type = "text/javascript">
<! --
/*
Two ways to add attributes to prototype
*/
// Method 1
Var myObj = function (){
This. study = "JavaScript ";
}
MyObj. prototype. holobby = function ()
{
This. holobby = "See girl ";
}
Var newObj = new myObj ();
For (var attr in newObj)
{
Document. write (attr + "<br/> ");
}
Document. write ("==============================< br/> ");
// Method 2
Var superObj = {name: "xugang "};
Var subObj = {age: 20 };
Function extend (superObj, subObj ){
// Obtain the prototype object of the parent object
SubObj. getSuper = superObj. prototype;
// Give the attributes of the parent object to the sub-Object
For (var I in superObj ){
SubObj [I] = superObj [I];
}
}
Extend (superObj, subObj );
For (var s in subObj)
{
Document. write (s + "<br/>"); // traverses attributes of sub-objects
}
// -->
</Script>
Running result:
Holobby
Study
================================
Age
GetSuper
Name