JavaScript的相关继承笔记以及使用外部库实现JavaScript的面向对象特性
- <script type="text/javascript">
- function Parent(pName){
- this.name = pName;
- this.sayMyName = function(){
- alert(this.name);
- };
- }
- function Mother(pName){
- this.name = pName;
- this.sayMyName = function(){
- alert(this.name+"sex*y");
- };
- }
- function Child(cName,cNumber){
- this.myFace = Parent;
- this.myFace(cName);
- delete this.myFace;
- this.myFace = Mother;
- this.myFace(cName);
- delete this.myFace;
- this.number = cNumber;
- this.sayNumber = function(){
- alert(this.number);
- };
- }
- var ObjP =new Parent("HideHai");
- var ObjM =new Mother("Hidewow");
- var ObjC =new Child("HideLow",3);
- ObjP.sayMyName();
- ObjM.sayMyName();
- ObjC.sayMyName();
- ObjC.sayNumber();
- </script>
- <script type="text/javascript">
- function Parent(pName){
- this.name = pName;
- this.sayMyName = function(){
- alert(this.name);
- };
- }
- function Mother(pName){
- this.name = pName;
- this.sayMyName = function(){
- alert(this.name+"sex*y");
- };
- }
- function Child(cName,cNumber){
- Parent.call(this,cName);
- Mother.call(this,cName);
- this.number = cNumber;
- this.sayNumber = function(){
- alert(this.number);
- };
- }
- var ObjP =new Parent("HideHai");
- var ObjM =new Mother("Hidewow");
- var ObjC =new Child("HideLow",3);
- ObjP.sayMyName();
- ObjM.sayMyName();
- ObjC.sayMyName();
- ObjC.sayNumber();
- </script>
- .... //代码同上
- function Child(cName,cNumber){
- Parent.apply(this,new Array(cName));
- Mother.call(this,new Array(cName));
- this.number = cNumber;
- this.sayNumber = function(){
- alert(this.number);
- };
- }
- <script type="text/javascript">
- function Parent(pName){
- this.name = pName;
- this.sayMyName = function(){
- alert(this.name);
- };
- }
- function Child(){
- }
- Child.prototype = new Parent();
- Child.prototype.name = "";
- Child.prototype.number = 0;
- Child.prototype.sayNumber = function(){
- alert(this.number);
- };
- var ObjP =new Parent("HideHai");
- //var ObjM =new Mother("Hidewow");
- var ObjC =new Child("HideLow");
- ObjC.name = "HideLow";
- ObjC.number = 3;
- ObjP.sayMyName();
- ObjC.sayMyName();
- ObjC.sayNumber();
- </script>
- <script type="text/javascript">
- function Parent(pName){
- this.name = pName;
- }
- Parent.prototype.sayMyName = function(){
- alert(this.name);
- };
- function Child(cName,cNumber){
- Parent.call(this,cName); //Parent.apply(this,new Aarray(cName));
- this.name = cName;
- }
- Child.prototype =new Parent();
- Child.prototype.sayNumber = function(){
- alert(this.number);
- };
- var ObjP =new Parent("HideHai");
- //var ObjM =new Mother("Hidewow");
- var ObjC =new Child("HideLow");
- ObjC.name = "HideLow";
- ObjC.number = 3;
- ObjP.sayMyName();
- ObjC.sayMyName();
- ObjC.sayNumber();
- </script>
- function Parent(pName){
- this.name = pName;
- if(typeof Parent._initialized == "undefined"){
- Parent.prototype.sayxName= function(){
- alert(this.name);
- };
- Parent._initialized = true;
- }
- }
- function Mother(pName){
- this.name = pName+"sex*y";
- if(typeof Mother._initialized == "undefined"){
- Parent.prototype.sayyName= function(){
- alert(this.name);
- };
- Mother._initialized = true;
- }
- }
- function Child(cName,cNumber){
- Parent.call(this,cName);
- Mother.call(this,cName);
- this.name = cName;
- if(typeof Parent._initialized == "undefined"){
- Child.prototype = inheritForm(Parent);
- Child.prototype = inheritForm(Mother);
- Child.prototype.sayNumber = function(){
- alert(this.number);
- };
- Child._initialized = true;
- }
- }
- _classes.registerClass("Grandpa");
- function Grandpa(pName){
- _classes.defineClass("Grandpa",prototypeFunction);
- this.init(pName); //init方法接受和构造函数相同的方法
- function prototypeFunction () {
- Grandpa.prototype.init = function (pName) {
- this.parentMethod("init"); //parentMethod方法可以传递多个参数,第一个参数为要调用的父类方法名字,然后其他参数被传递给此方法
- this.name = pName;
- }
- Grandpa.prototype.saynName = function () {
- alert(this.name);
- }
- }
- }
- _classes.registerClass("Parent","Grandpa");
- function Parent(pName,cNumber){
- _classes.defineClass("Parent",prototypeFunction);
- this.init(pName,cNumber); //init方法接受和构造函数相同的方法
- function prototypeFunction () {
- Parent.prototype.init = function (pName,cNumber) {
- this.parentMethod("init");
- this.name = pName;
- this.number = cNumber;
- }
- Parent.prototype.sayMyName = function () {
- alert(this.name);
- }
- Parent.prototype.sayMyNumber = function () {
- alert(this.number);
- }
- }
- }
本文出自 “HideHai's Blog” 博客,请务必保留此出处http://hidehai.blog.51cto.com/307955/199163
要是有更多自己的东西就好了